home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / xdisp.c < prev    next >
C/C++ Source or Header  |  1993-10-15  |  90KB  |  2,940 lines

  1. /* Display generation from window structure and buffer text.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include <stdio.h>
  23. /*#include <ctype.h>*/
  24. #undef NULL
  25. #include "lisp.h"
  26. #include "frame.h"
  27. #include "window.h"
  28. #include "termchar.h"
  29. #include "dispextern.h"
  30. #include "buffer.h"
  31. #include "indent.h"
  32. #include "commands.h"
  33. #include "macros.h"
  34. #include "disptab.h"
  35. #include "termhooks.h"
  36.  
  37. #ifdef STDC_HEADERS
  38. #include <stdlib.h>
  39. #endif
  40. #include "xdisp_p.h"
  41. #include "dispnew_d.h"
  42. #include "doprnt_p.h"
  43. #include "sysdep_p.h"
  44. #include "intervals_p.h"
  45. #include "scroll_p.h"
  46. static void echo_area_display _P_((void));
  47. static void redisplay_windows _P_((Lisp_Object window));
  48. static void redisplay_window _P_((Lisp_Object window, int just_this_one));
  49. static void try_window _P_((Lisp_Object window, register int pos));
  50. static int try_window_id _P_((Lisp_Object window));
  51. static struct position * display_text_line _P_((struct window *w, int start,
  52.                                                 int vpos, int hpos,
  53.                                                 int taboffset));
  54. static void display_menu_bar _P_((struct window *w));
  55. static void display_mode_line _P_((struct window *w));
  56. static int display_mode_element _P_((struct window *w, register int vpos,
  57.                                      register int hpos, int depth,
  58.                                      int minendcol, register int maxendcol,
  59.                                      register Lisp_Object elt));
  60. static char * decode_mode_spec _P_((struct window *w, register char c,
  61.                                     register int maxwidth));
  62. static int display_count_lines _P_((int from, int limit, int n, int *pos_ptr));
  63. static int display_string _P_((struct window *w, int vpos,
  64.                                unsigned char *string, int hpos, GLYPH truncate,
  65.                                int mincol, int maxcol));
  66.  
  67. extern int interrupt_input;
  68. extern int command_loop_level;
  69.  
  70. /* Nonzero means print newline before next minibuffer message.  */
  71.  
  72. int noninteractive_need_newline;
  73.  
  74. #ifndef min
  75. #define min(a, b) ((a) < (b) ? (a) : (b))
  76. #endif
  77. #ifndef max
  78. #define max(a, b) ((a) > (b) ? (a) : (b))
  79. #endif
  80.  
  81. /* The buffer position of the first character appearing
  82.  entirely or partially on the current frame line.
  83.  Or zero, which disables the optimization for the current frame line. */
  84. static int this_line_bufpos;
  85.  
  86. /* Number of characters past the end of this line,
  87.    including the terminating newline */
  88. static int this_line_endpos;
  89.  
  90. /* The vertical position of this frame line. */
  91. static int this_line_vpos;
  92.  
  93. /* Hpos value for start of display on this frame line.
  94.    Usually zero, but negative if first character really began
  95.    on previous line */
  96. static int this_line_start_hpos;
  97.  
  98. /* Buffer that this_line variables are describing. */
  99. static struct buffer *this_line_buffer;
  100.  
  101. /* Set by try_window_id to the vpos of first of any lines
  102.    scrolled on to the bottom of the frame.  These lines should
  103.    not be included in any general scroll computation.  */
  104. static int scroll_bottom_vpos;
  105.  
  106. /* Value of echo_area_glyphs when it was last acted on.
  107.   If this is nonzero, there is a message on the frame
  108.   in the minibuffer and it should be erased as soon
  109.   as it is no longer requested to appear. */
  110. char *previous_echo_glyphs;
  111.  
  112. /* Nonzero means truncate lines in all windows less wide than the frame */
  113. int truncate_partial_width_windows;
  114.  
  115. Lisp_Object Vglobal_mode_string;
  116.  
  117. /* Marker for where to display an arrow on top of the buffer text.  */
  118. Lisp_Object Voverlay_arrow_position;
  119.  
  120. /* String to display for the arrow.  */
  121. Lisp_Object Voverlay_arrow_string;
  122.  
  123. /* Values of those variables at last redisplay.  */
  124. static Lisp_Object last_arrow_position, last_arrow_string;
  125.  
  126. /* Nonzero if overlay arrow has been displayed once in this window.  */
  127. static int overlay_arrow_seen;
  128.  
  129. /* Nonzero means highlight the region even in nonselected windows.  */
  130. static int highlight_nonselected_windows;
  131.  
  132. /* If cursor motion alone moves point off frame,
  133.    Try scrolling this many lines up or down if that will bring it back.  */
  134. int scroll_step;
  135.  
  136. /* Nonzero if try_window_id has made blank lines at window bottom
  137.  since the last redisplay that paused */
  138. static int blank_end_of_window;
  139.  
  140. /* Number of windows showing the buffer of the selected window.
  141.    keyboard.c refers to this.  */
  142. int buffer_shared;
  143.  
  144. /* display_text_line sets these to the frame position (origin 0) of point,
  145.    whether the window is selected or not.
  146.    Set one to -1 first to determine whether point was found afterwards.  */
  147.  
  148. static int cursor_vpos;
  149. static int cursor_hpos;
  150.  
  151. int debug_end_pos;
  152.  
  153. /* Nonzero means display mode line highlighted */
  154. int mode_line_inverse_video;
  155.  
  156. /* Prompt to display in front of the minibuffer contents */
  157. char *minibuf_prompt;
  158.  
  159. /* Width in columns of current minibuffer prompt.  */
  160. int minibuf_prompt_width;
  161.  
  162. /* Message to display instead of minibuffer contents
  163.    This is what the functions error and message make,
  164.    and command echoing uses it as well.
  165.    It overrides the minibuf_prompt as well as the buffer.  */
  166. char *echo_area_glyphs;
  167.  
  168. /* true iff we should redraw the mode lines on the next redisplay */
  169. int update_mode_lines;
  170.  
  171. /* Smallest number of characters before the gap
  172.    at any time since last redisplay that finished.
  173.    Valid for current buffer when try_window_id can be called.  */
  174. int beg_unchanged;
  175.  
  176. /* Smallest number of characters after the gap
  177.    at any time since last redisplay that finished.
  178.    Valid for current buffer when try_window_id can be called.  */
  179. int end_unchanged;
  180.  
  181. /* MODIFF as of last redisplay that finished;
  182.    if it matches MODIFF, beg_unchanged and end_unchanged
  183.    contain no useful information */
  184. int unchanged_modified;
  185.  
  186. /* Nonzero if head_clip or tail_clip of current buffer has changed
  187.    since last redisplay that finished */
  188. int clip_changed;
  189.  
  190. /* Nonzero if window sizes or contents have changed
  191.    since last redisplay that finished */
  192. int windows_or_buffers_changed;
  193.  
  194. /* Nonzero after display_mode_line if %l was used
  195.    and it displayed a line number.  */
  196. int line_number_displayed;
  197.  
  198. /* Maximum buffer size for which to display line numbers.  */
  199. int line_number_display_limit;
  200.  
  201. /* Specify m, a string, as a message in the minibuf.  If m is 0, clear out
  202.    any existing message, and let the minibuffer text show through.  */
  203. void
  204. message1 (m)
  205.      char *m;
  206. {
  207.   if (noninteractive)
  208.     {
  209.       if (noninteractive_need_newline)
  210.     putc ('\n', stderr);
  211.       noninteractive_need_newline = 0;
  212.       if (cursor_in_echo_area != 0)
  213.     fprintf (stderr, "%s", m);
  214.       else
  215.     fprintf (stderr, "%s\n", m);
  216.       fflush (stderr);
  217.     }
  218.   /* A null message buffer means that the frame hasn't really been
  219.      initialized yet.  Error messages get reported properly by
  220.      cmd_error, so this must be just an informative message; toss it.  */
  221.   else if (INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
  222.     {
  223. #ifdef MULTI_FRAME
  224.       Lisp_Object minibuf_frame;
  225.  
  226.       choose_minibuf_frame ();
  227.       minibuf_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
  228.       FRAME_SAMPLE_VISIBILITY (XFRAME (minibuf_frame));
  229.       if (FRAME_VISIBLE_P (selected_frame)
  230.       && ! FRAME_VISIBLE_P (XFRAME (minibuf_frame)))
  231.     Fmake_frame_visible (WINDOW_FRAME (XWINDOW (minibuf_window)));
  232. #endif
  233.  
  234.       if (m)
  235.     echo_area_glyphs = m;
  236.       else
  237.     echo_area_glyphs = previous_echo_glyphs = 0;
  238.  
  239.       do_pending_window_change ();
  240.       echo_area_display ();
  241.       update_frame (XFRAME (XWINDOW (minibuf_window)->frame), 1, 1);
  242.       do_pending_window_change ();
  243.     }
  244. }
  245.  
  246. /* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by print;
  247.    zero if being used by message.  */
  248. int message_buf_print;
  249.  
  250. /* Dump an informative message to the minibuf.  If m is 0, clear out
  251.    any existing message, and let the minibuffer text show through.  */
  252. /* VARARGS 1 */
  253. void _VARARGS_
  254. #ifdef USE_PROTOTYPES
  255. #include <stdarg.h>
  256. message (char *m, ...)
  257. #else
  258. message (m, a1, a2, a3)
  259.      char *m;
  260. #endif    
  261. {
  262. #ifdef USE_PROTOTYPES
  263.   va_list args;
  264.  
  265.   va_start(args, m);
  266. #endif  
  267.   if (noninteractive)
  268.     {
  269.       if (m)
  270.     {
  271.       if (noninteractive_need_newline)
  272.         putc ('\n', stderr);
  273.       noninteractive_need_newline = 0;
  274. #ifdef USE_PROTOTYPES          
  275.       vfprintf (stderr, m, args);
  276.           va_end(args);
  277. #else          
  278.       fprintf (stderr, m, a1, a2, a3);
  279. #endif          
  280.       if (cursor_in_echo_area == 0)
  281.         fprintf (stderr, "\n");
  282.       fflush (stderr);
  283.     }
  284.     }
  285.   else if (INTERACTIVE)
  286.     {
  287.       /* The frame whose minibuffer we're going to display the message on.
  288.      It may be larger than the selected frame, so we need
  289.      to use its buffer, not the selected frame's buffer.  */
  290.       FRAME_PTR echo_frame;
  291. #ifdef MULTI_FRAME
  292.       choose_minibuf_frame ();
  293.       echo_frame = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
  294. #else
  295.       echo_frame = selected_frame;
  296. #endif
  297.  
  298.       /* A null message buffer means that the frame hasn't really been
  299.      initialized yet.  Error messages get reported properly by
  300.      cmd_error, so this must be just an informative message; toss it.  */
  301.       if (FRAME_MESSAGE_BUF (echo_frame))
  302.     {
  303.       if (m)
  304.         {
  305.           {
  306. #ifdef USE_PROTOTYPES                  
  307.                 char *a[3];
  308.  
  309.                 a[0] = va_arg(args, char *);
  310.                 a[1] = va_arg(args, char *);
  311.                 a[2] = va_arg(args, char *);
  312.                 va_end(args);
  313.         doprnt (FRAME_MESSAGE_BUF (echo_frame),
  314.             FRAME_WIDTH (echo_frame), m, 0, 3, a);
  315. #elif NO_ARG_ARRAY
  316.         int a[3];
  317.         a[0] = a1;
  318.         a[1] = a2;
  319.         a[2] = a3;
  320.  
  321.         doprnt (FRAME_MESSAGE_BUF (echo_frame),
  322.             FRAME_WIDTH (echo_frame), m, 0, 3, a);
  323. #else
  324.         doprnt (FRAME_MESSAGE_BUF (echo_frame),
  325.             FRAME_WIDTH (echo_frame), m, 0, 3, &a1);
  326. #endif /* NO_ARG_ARRAY */
  327.           }
  328.  
  329.           message1 (FRAME_MESSAGE_BUF (echo_frame));
  330.         }
  331.       else
  332.         message1 (0);
  333.  
  334.       /* Print should start at the beginning of the message
  335.          buffer next time.  */
  336.       message_buf_print = 0;
  337.     }
  338.     }
  339. }
  340.  
  341. static void
  342. echo_area_display ()
  343. {
  344.   register int vpos;
  345.   FRAME_PTR f;
  346.  
  347. #ifdef MULTI_FRAME
  348.   choose_minibuf_frame ();
  349. #endif
  350.  
  351.   f = XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
  352.  
  353.   if (! FRAME_VISIBLE_P (f))
  354.     return;
  355.  
  356.   if (frame_garbaged)
  357.     {
  358.       redraw_garbaged_frames ();
  359.       frame_garbaged = 0;
  360.     }
  361.  
  362.   if (echo_area_glyphs || minibuf_level == 0)
  363.     {
  364.       vpos = XFASTINT (XWINDOW (minibuf_window)->top);
  365.       get_display_line (f, vpos, 0);
  366.       display_string (XWINDOW (minibuf_window), vpos,
  367.               echo_area_glyphs ? echo_area_glyphs : "",
  368.               0, 0, 0, FRAME_WIDTH (f));
  369.  
  370.       /* If desired cursor location is on this line, put it at end of text */
  371.       if (FRAME_CURSOR_Y (f) == vpos)
  372.     FRAME_CURSOR_X (f) = FRAME_DESIRED_GLYPHS (f)->used[vpos];
  373.  
  374.       /* Fill the rest of the minibuffer window with blank lines.  */
  375.       {
  376.     int i;
  377.  
  378.     for (i = vpos + 1;
  379.          i < vpos + XFASTINT (XWINDOW (minibuf_window)->height); i++)
  380.       {
  381.         get_display_line (f, i, 0);
  382.         display_string (XWINDOW (minibuf_window), vpos,
  383.                 "", 0, 0, 0, FRAME_WIDTH (f));
  384.       }
  385.       }
  386.     }
  387.   else if (!EQ (minibuf_window, selected_window))
  388.     windows_or_buffers_changed++;
  389.  
  390.   if (EQ (minibuf_window, selected_window))
  391.     this_line_bufpos = 0;
  392.  
  393.   previous_echo_glyphs = echo_area_glyphs;
  394. }
  395.  
  396. /* Do a frame update, taking possible shortcuts into account.
  397.    This is the main external entry point for redisplay.
  398.  
  399.    If the last redisplay displayed an echo area message and that
  400.    message is no longer requested, we clear the echo area
  401.    or bring back the minibuffer if that is in use.
  402.  
  403.    Everyone would like to have a hook here to call eval,
  404.    but that cannot be done safely without a lot of changes elsewhere.
  405.    This can be called from signal handlers; with alarms set up;
  406.    or with synchronous processes running.
  407.    See the function `echo' in keyboard.c.
  408.    See Fcall_process; if you called it from here, it could be
  409.    entered recursively.  */
  410.  
  411. void
  412. redisplay ()
  413. {
  414.   register struct window *w = XWINDOW (selected_window);
  415.   register int pause;
  416.   int must_finish = 0;
  417.   int all_windows;
  418.   register int tlbufpos, tlendpos;
  419.   struct position pos;
  420.   extern int input_pending;
  421.  
  422.   if (noninteractive)
  423.     return;
  424.  
  425.   /* Set the visible flags for all frames.
  426.      Do this before checking for resized or garbaged frames; they want
  427.      to know if their frames are visible.
  428.      See the comment in frame.h for FRAME_SAMPLE_VISIBILITY.  */
  429.   {
  430.     Lisp_Object tail, frame;
  431.  
  432.     FOR_EACH_FRAME (tail, frame)
  433.       FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
  434.   }
  435.  
  436.   /* Notice any pending interrupt request to change frame size.  */
  437.   do_pending_window_change ();
  438.  
  439.   if (frame_garbaged)
  440.     {
  441.       redraw_garbaged_frames ();
  442.       frame_garbaged = 0;
  443.     }
  444.  
  445.   /* Normally the message* functions will have already displayed and
  446.      updated the echo area, but the frame may have been trashed, or
  447.      the update may have been preempted, so display the echo area
  448.      again here.  */
  449.   if (echo_area_glyphs || previous_echo_glyphs)
  450.     {
  451.       echo_area_display ();
  452.       must_finish = 1;
  453.     }
  454.  
  455.   if (clip_changed || windows_or_buffers_changed)
  456.     update_mode_lines++;
  457.  
  458.   /* Detect case that we need to write a star in the mode line.  */
  459.   if (XFASTINT (w->last_modified) < MODIFF
  460.       && XFASTINT (w->last_modified) <= current_buffer->save_modified)
  461.     {
  462.       w->update_mode_line = Qt;
  463.       if (buffer_shared > 1)
  464.     update_mode_lines++;
  465.     }
  466.  
  467.   FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
  468.  
  469.   all_windows = update_mode_lines || buffer_shared > 1;
  470.  
  471.   /* If specs for an arrow have changed, do thorough redisplay
  472.      to ensure we remove any arrow that should no longer exist.  */
  473.   if (! EQ (Voverlay_arrow_position, last_arrow_position)
  474.       || ! EQ (Voverlay_arrow_string, last_arrow_string))
  475.     all_windows = 1, clip_changed = 1;
  476.  
  477.   /* If showing region, and mark has changed, must redisplay whole window.  */
  478.   if (((!NILP (Vtransient_mark_mode)
  479.     && !NILP (XBUFFER (w->buffer)->mark_active))
  480.        != !NILP (w->region_showing))
  481.       || (!NILP (w->region_showing)
  482.       && !EQ (w->region_showing,
  483.           Fmarker_position (XBUFFER (w->buffer)->mark))))
  484.     this_line_bufpos = -1;
  485.  
  486.   tlbufpos = this_line_bufpos;
  487.   tlendpos = this_line_endpos;
  488.   if (!all_windows && tlbufpos > 0 && NILP (w->update_mode_line)
  489.       && FRAME_VISIBLE_P (XFRAME (w->frame))
  490.       /* Make sure recorded data applies to current buffer, etc */
  491.       && this_line_buffer == current_buffer
  492.       && current_buffer == XBUFFER (w->buffer)
  493.       && NILP (w->force_start)
  494.       /* Point must be on the line that we have info recorded about */
  495.       && point >= tlbufpos
  496.       && point <= Z - tlendpos
  497.       /* All text outside that line, including its final newline,
  498.      must be unchanged */
  499.       && (XFASTINT (w->last_modified) >= MODIFF
  500.       || (beg_unchanged >= tlbufpos - 1
  501.           && GPT >= tlbufpos
  502.           /* If selective display, can't optimize
  503.          if the changes start at the beginning of the line.  */
  504.           && ((XTYPE (current_buffer->selective_display) == Lisp_Int
  505.            && XINT (current_buffer->selective_display) > 0
  506.            ? (beg_unchanged >= tlbufpos
  507.               && GPT > tlbufpos)
  508.            : 1))
  509.           && end_unchanged >= tlendpos
  510.           && Z - GPT >= tlendpos)))
  511.     {
  512.       if (tlbufpos > BEGV && FETCH_CHAR (tlbufpos - 1) != '\n'
  513.       && (tlbufpos == ZV
  514.           || FETCH_CHAR (tlbufpos) == '\n'))
  515.     /* Former continuation line has disappeared by becoming empty */
  516.     goto cancel;
  517.       else if (XFASTINT (w->last_modified) < MODIFF
  518.            || MINI_WINDOW_P (w))
  519.     {
  520.       cursor_vpos = -1;
  521.       overlay_arrow_seen = 0;
  522.       display_text_line (w, tlbufpos, this_line_vpos, this_line_start_hpos,
  523.                  pos_tab_offset (w, tlbufpos));
  524.       /* If line contains point, is not continued,
  525.          and ends at same distance from eob as before, we win */
  526.       if (cursor_vpos >= 0 && this_line_bufpos
  527.           && this_line_endpos == tlendpos)
  528.         {
  529.           if (XFASTINT (w->width) != FRAME_WIDTH (XFRAME (WINDOW_FRAME (w))))
  530.         preserve_other_columns (w);
  531.           goto update;
  532.         }
  533.       else
  534.         goto cancel;
  535.     }
  536.       else if (point == XFASTINT (w->last_point))
  537.     {
  538.       if (!must_finish)
  539.         {
  540.           do_pending_window_change ();
  541.           return;
  542.         }
  543.       goto update;
  544.     }
  545.       /* If highlighting the region, we can't just move the cursor.  */
  546.       else if (! (!NILP (Vtransient_mark_mode)
  547.           && !NILP (current_buffer->mark_active))
  548.            && NILP (w->region_showing))
  549.     {
  550.       pos = *compute_motion (tlbufpos, 0,
  551.                  XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0,
  552.                  point, 2, - (1 << (SHORTBITS - 1)),
  553.                  window_internal_width (w) - 1,
  554.                  XINT (w->hscroll),
  555.                  pos_tab_offset (w, tlbufpos));
  556.       if (pos.vpos < 1)
  557.         {
  558.           FRAME_CURSOR_X (selected_frame)
  559.         = XFASTINT (w->left) + max (pos.hpos, 0);
  560.           FRAME_CURSOR_Y (selected_frame) = this_line_vpos;
  561.           goto update;
  562.         }
  563.       else
  564.         goto cancel;
  565.     }
  566.     cancel:
  567.       /* Text changed drastically or point moved off of line */
  568.       cancel_line (this_line_vpos, selected_frame);
  569.     }
  570.  
  571.   this_line_bufpos = 0;
  572.   all_windows |= buffer_shared > 1;
  573.  
  574.   if (all_windows)
  575.     {
  576.       Lisp_Object tail, frame;
  577.  
  578. #ifdef HAVE_X_WINDOWS
  579.       /* Since we're doing a thorough redisplay, we might as well
  580.      recompute all our display faces.  */
  581.       clear_face_vector ();
  582. #endif
  583.  
  584.       /* Recompute # windows showing selected buffer.
  585.      This will be incremented each time such a window is displayed.  */
  586.       buffer_shared = 0;
  587.  
  588.       FOR_EACH_FRAME (tail, frame)
  589.     {
  590.       FRAME_PTR f = XFRAME (frame);
  591.  
  592.       /* Mark all the scroll bars to be removed; we'll redeem the ones
  593.          we want when we redisplay their windows.  */
  594.       if (condemn_scroll_bars_hook)
  595.         (*condemn_scroll_bars_hook) (f);
  596.  
  597.       if (FRAME_VISIBLE_P (f))
  598.         redisplay_windows (FRAME_ROOT_WINDOW (f));
  599.  
  600.       /* Any scroll bars which redisplay_windows should have nuked
  601.          should now go away.  */
  602.       if (judge_scroll_bars_hook)
  603.         (*judge_scroll_bars_hook) (f);
  604.     }
  605.     }
  606.   else if (FRAME_VISIBLE_P (selected_frame))
  607.     {
  608.       redisplay_window (selected_window, 1);
  609.       if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
  610.     preserve_other_columns (w);
  611.     }
  612.  
  613. update: 
  614.   /* Prevent various kinds of signals during display update.
  615.      stdio is not robust about handling signals,
  616.      which can cause an apparent I/O error.  */
  617.   if (interrupt_input)
  618.     unrequest_sigio ();
  619.   stop_polling ();
  620.  
  621. #ifdef MULTI_FRAME
  622.   if (all_windows)
  623.     {
  624.       Lisp_Object tail;
  625.  
  626.       pause = 0;
  627.  
  628.       for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
  629.     {
  630.       FRAME_PTR f;
  631.  
  632.       if (XTYPE (XCONS (tail)->car) != Lisp_Frame)
  633.         continue;
  634.  
  635.       f = XFRAME (XCONS (tail)->car);
  636.       if (FRAME_VISIBLE_P (f))
  637.         {
  638.           pause |= update_frame (f, 0, 0);
  639.           if (!pause)
  640.         mark_window_display_accurate (f->root_window, 1);
  641.         }
  642.     }
  643.     }
  644.   else
  645. #endif /* MULTI_FRAME */
  646.     {
  647.       if (FRAME_VISIBLE_P (selected_frame))
  648.     pause = update_frame (selected_frame, 0, 0);
  649.  
  650.       /* We may have called echo_area_display at the top of this
  651.      function.  If the echo area is on another frame, that may
  652.      have put text on a frame other than the selected one, so the
  653.      above call to update_frame would not have caught it.  Catch
  654.      it here.  */
  655.       {
  656.     FRAME_PTR mini_frame =
  657.       XFRAME (WINDOW_FRAME (XWINDOW (minibuf_window)));
  658.     
  659.     if (mini_frame != selected_frame)
  660.       pause |= update_frame (mini_frame, 0, 0);
  661.       }
  662.     }
  663.  
  664.   /* If frame does not match, prevent doing single-line-update next time.
  665.      Also, don't forget to check every line to update the arrow.  */
  666.   if (pause)
  667.     {
  668.       this_line_bufpos = 0;
  669.       if (!NILP (last_arrow_position))
  670.     {
  671.       last_arrow_position = Qt;
  672.       last_arrow_string = Qt;
  673.     }
  674.       /* If we pause after scrolling, some lines in current_frame
  675.      may be null, so preserve_other_columns won't be able to
  676.      preserve all the vertical-bar separators.  So, avoid using it
  677.      in that case.  */
  678.       if (XFASTINT (w->width) != FRAME_WIDTH (selected_frame))
  679.     update_mode_lines = 1;
  680.     }
  681.  
  682.   /* Now text on frame agrees with windows, so
  683.      put info into the windows for partial redisplay to follow */
  684.  
  685.   if (!pause)
  686.     {
  687.       register struct buffer *b = XBUFFER (w->buffer);
  688.  
  689.       blank_end_of_window = 0;
  690.       clip_changed = 0;
  691.       unchanged_modified = BUF_MODIFF (b);
  692.       beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
  693.       end_unchanged = BUF_Z (b) - BUF_GPT (b);
  694.  
  695.       XFASTINT (w->last_point) = BUF_PT (b);
  696.       XFASTINT (w->last_point_x) = FRAME_CURSOR_X (selected_frame);
  697.       XFASTINT (w->last_point_y) = FRAME_CURSOR_Y (selected_frame);
  698.  
  699.       if (all_windows)
  700.     mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
  701.       else
  702.     {
  703.       w->update_mode_line = Qnil;
  704.       XFASTINT (w->last_modified) = BUF_MODIFF (b);
  705.       w->window_end_valid = Qt;
  706.       last_arrow_position = Voverlay_arrow_position;
  707.       last_arrow_string = Voverlay_arrow_string;
  708.     }
  709.       update_mode_lines = 0;
  710.       windows_or_buffers_changed = 0;
  711.     }
  712.  
  713.   /* Start SIGIO interrupts coming again.
  714.      Having them off during the code above
  715.      makes it less likely one will discard output,
  716.      but not impossible, since there might be stuff
  717.      in the system buffer here.
  718.      But it is much hairier to try to do anything about that.  */
  719.  
  720.   if (interrupt_input)
  721.     request_sigio ();
  722.   start_polling ();
  723.  
  724.   /* Change frame size now if a change is pending.  */
  725.   do_pending_window_change ();
  726. }
  727.  
  728. /* Redisplay, but leave alone any recent echo area message
  729.    unless another message has been requested in its place.
  730.  
  731.    This is useful in situations where you need to redisplay but no
  732.    user action has occurred, making it inappropriate for the message
  733.    area to be cleared.  See tracking_off and
  734.    wait_reading_process_input for examples of these situations.  */
  735.  
  736. _VOID_
  737. redisplay_preserve_echo_area ()
  738. {
  739.   if (echo_area_glyphs == 0 && previous_echo_glyphs != 0)
  740.     {
  741.       echo_area_glyphs = previous_echo_glyphs;
  742.       redisplay ();
  743.       echo_area_glyphs = 0;
  744.     }
  745.   else
  746.     redisplay ();
  747. }
  748.  
  749. void
  750. mark_window_display_accurate (window, flag)
  751.      Lisp_Object window;
  752.      int flag;
  753. {
  754.   register struct window *w;
  755.  
  756.   for (;!NILP (window); window = w->next)
  757.     {
  758.       if (XTYPE (window) != Lisp_Window) abort ();
  759.       w = XWINDOW (window);
  760.  
  761.       if (!NILP (w->buffer))
  762.     {
  763.       XFASTINT (w->last_modified)
  764.         = !flag ? 0
  765.           : XBUFFER (w->buffer) == current_buffer
  766.         ? MODIFF : BUF_MODIFF (XBUFFER (w->buffer));
  767.  
  768.       /* Record if we are showing a region, so can make sure to
  769.          update it fully at next redisplay.  */
  770.       w->region_showing = (!NILP (Vtransient_mark_mode)
  771.                    && !NILP (XBUFFER (w->buffer)->mark_active)
  772.                    ? Fmarker_position (XBUFFER (w->buffer)->mark)
  773.                    : Qnil);
  774.     }
  775.  
  776.       w->window_end_valid = Qt;
  777.       w->update_mode_line = Qnil;
  778.  
  779.       if (!NILP (w->vchild))
  780.     mark_window_display_accurate (w->vchild, flag);
  781.       if (!NILP (w->hchild))
  782.     mark_window_display_accurate (w->hchild, flag);
  783.     }
  784.  
  785.   if (flag)
  786.     {
  787.       last_arrow_position = Voverlay_arrow_position;
  788.       last_arrow_string = Voverlay_arrow_string;
  789.     }
  790.   else
  791.     {
  792.       /* t is unequal to any useful value of Voverlay_arrow_... */
  793.       last_arrow_position = Qt;
  794.       last_arrow_string = Qt;
  795.     }
  796. }
  797.  
  798. int do_id = 1;
  799.  
  800. static void
  801. redisplay_windows (window)
  802.      Lisp_Object window;
  803. {
  804.   for (; !NILP (window); window = XWINDOW (window)->next)
  805.     redisplay_window (window, 0);
  806. }
  807.  
  808. static void
  809. redisplay_window (window, just_this_one)
  810.      Lisp_Object window;
  811.      int just_this_one;
  812. {
  813.   register struct window *w = XWINDOW (window);
  814.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  815.   int height;
  816.   register int lpoint = point;
  817.   struct buffer *old = current_buffer;
  818.   register int width = window_internal_width (w) - 1;
  819.   register int startp;
  820.   register int hscroll = XINT (w->hscroll);
  821.   struct position pos;
  822.   int opoint = point;
  823.   int tem;
  824.  
  825.   if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
  826.  
  827.   /* If this is a combination window, do its children; that's all.  */
  828.  
  829.   if (!NILP (w->vchild))
  830.     {
  831.       redisplay_windows (w->vchild);
  832.       return;
  833.     }
  834.   if (!NILP (w->hchild))
  835.     {
  836.       redisplay_windows (w->hchild);
  837.       return;
  838.     }
  839.   if (NILP (w->buffer))
  840.     abort ();
  841.   
  842.   height = window_internal_height (w);
  843.  
  844.   if (MINI_WINDOW_P (w))
  845.     {
  846.       if (w == XWINDOW (minibuf_window))
  847.     {
  848.       if (echo_area_glyphs)
  849.         /* We've already displayed the echo area glyphs, if any.  */
  850.         goto finish_scroll_bars;
  851.     }
  852.       else
  853.     {
  854.       /* This is a minibuffer, but it's not the currently active one, so
  855.          clear it.  */
  856.       int vpos = XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
  857.       int i;
  858.  
  859.       for (i = 0; i < height; i++)
  860.         {
  861.           get_display_line (f, vpos + i, 0);
  862.           display_string (w, vpos + i, "", 0, 0, 0, width);
  863.         }
  864.       
  865.       goto finish_scroll_bars;
  866.     }
  867.     }
  868.  
  869.   if (update_mode_lines)
  870.     w->update_mode_line = Qt;
  871.  
  872.   /* Otherwise set up data on this window; select its buffer and point value */
  873.  
  874.   current_buffer = XBUFFER (w->buffer);
  875.   opoint = point;
  876.  
  877.   /* Count number of windows showing the selected buffer.  */
  878.  
  879.   if (!just_this_one
  880.       && current_buffer == XBUFFER (XWINDOW (selected_window)->buffer))
  881.     buffer_shared++;
  882.  
  883.   /* POINT refers normally to the selected window.
  884.      For any other window, set up appropriate value.  */
  885.  
  886.   if (!EQ (window, selected_window))
  887.     {
  888.       SET_PT (marker_position (w->pointm));
  889.       if (point < BEGV)
  890.     {
  891.       SET_PT (BEGV);
  892.       Fset_marker (w->pointm, make_number (point), Qnil);
  893.     }
  894.       else if (point > (ZV - 1))
  895.     {
  896.       SET_PT (ZV);
  897.       Fset_marker (w->pointm, make_number (point), Qnil);
  898.     }
  899.     }
  900.  
  901.   /* If window-start is screwed up, choose a new one.  */
  902.   if (XMARKER (w->start)->buffer != current_buffer)
  903.     goto recenter;
  904.  
  905.   startp = marker_position (w->start);
  906.  
  907.   /* Handle case where place to start displaying has been specified,
  908.      unless the specified location is outside the accessible range.  */
  909.   if (!NILP (w->force_start))
  910.     {
  911.       /* Forget any recorded base line for line number display.  */
  912.       w->base_line_number = Qnil;
  913.       w->update_mode_line = Qt;
  914.       w->force_start = Qnil;
  915.       XFASTINT (w->last_modified) = 0;
  916.       if (startp < BEGV) startp = BEGV;
  917.       if (startp > ZV)   startp = ZV;
  918.       try_window (window, startp);
  919.       if (cursor_vpos < 0)
  920.     {
  921.       /* ??? What should happen here if highlighting a region?  */
  922.       /* If point does not appear, move point so it does appear */
  923.       pos = *compute_motion (startp, 0,
  924.                 ((EQ (window, minibuf_window) && startp == 1)
  925.                  ? minibuf_prompt_width : 0)
  926.                 +
  927.                 (hscroll ? 1 - hscroll : 0),
  928.                 ZV, height / 2,
  929.                 - (1 << (SHORTBITS - 1)),
  930.                 width, hscroll, pos_tab_offset (w, startp));
  931.       SET_PT (pos.bufpos);
  932.       if (w != XWINDOW (FRAME_SELECTED_WINDOW (f)))
  933.         Fset_marker (w->pointm, make_number (point), Qnil);
  934.       else
  935.         {
  936.           lpoint = point;
  937.           FRAME_CURSOR_X (f) = max (0, pos.hpos) + XFASTINT (w->left);
  938.           FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
  939.         }
  940.     }
  941.       goto done;
  942.     }
  943.  
  944.   /* Handle case where text has not changed, only point,
  945.      and it has not moved off the frame */
  946.  
  947.   /* This code is not used for minibuffer for the sake of
  948.      the case of redisplaying to replace an echo area message;
  949.      since in that case the minibuffer contents per se are usually unchanged.
  950.      This code is of no real use in the minibuffer since
  951.      the handling of this_line_bufpos, etc.,
  952.      in redisplay handles the same cases.  */
  953.  
  954.   if (XFASTINT (w->last_modified) >= MODIFF
  955.       && point >= startp && !clip_changed
  956.       && (just_this_one || XFASTINT (w->width) == FRAME_WIDTH (f))
  957.       /* Can't use this case if highlighting a region.  */
  958.       && !(!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
  959.       && NILP (w->region_showing)
  960.       && !EQ (window, minibuf_window))
  961.     {
  962.       pos = *compute_motion (startp, 0, (hscroll ? 1 - hscroll : 0),
  963.                 point, height + 1, 10000, width, hscroll,
  964.                 pos_tab_offset (w, startp));
  965.  
  966.       if (pos.vpos < height)
  967.     {
  968.       /* Ok, point is still on frame */
  969.       if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
  970.         {
  971.           /* These variables are supposed to be origin 1 */
  972.           FRAME_CURSOR_X (f) = max (0, pos.hpos) + XFASTINT (w->left);
  973.           FRAME_CURSOR_Y (f) = pos.vpos + XFASTINT (w->top);
  974.         }
  975.       /* This doesn't do the trick, because if a window to the right of
  976.          this one must be redisplayed, this does nothing because there
  977.          is nothing in DesiredFrame yet, and then the other window is
  978.          redisplayed, making likes that are empty in this window's columns.
  979.          if (XFASTINT (w->width) != FRAME_WIDTH (f))
  980.          preserve_my_columns (w);
  981.          */
  982.       goto done;
  983.     }
  984.       /* Don't bother trying redisplay with same start;
  985.     we already know it will lose */
  986.     }
  987.   /* If current starting point was originally the beginning of a line
  988.      but no longer is, find a new starting point.  */
  989.   else if (!NILP (w->start_at_line_beg)
  990.        && !(startp == BEGV
  991.         || FETCH_CHAR (startp - 1) == '\n'))
  992.     {
  993.       goto recenter;
  994.     }
  995.   else if (just_this_one && !MINI_WINDOW_P (w)
  996.        && point >= startp
  997.        && XFASTINT (w->last_modified)
  998.        /* or else vmotion on first line won't work.  */
  999.        && ! NILP (w->start_at_line_beg)
  1000.        && ! EQ (w->window_end_valid, Qnil)
  1001.        && do_id && !clip_changed
  1002.        && !blank_end_of_window
  1003.        && XFASTINT (w->width) == FRAME_WIDTH (f)
  1004.        /* Can't use this case if highlighting a region.  */
  1005.        && !(!NILP (Vtransient_mark_mode)
  1006.         && !NILP (current_buffer->mark_active))
  1007.        && NILP (w->region_showing)
  1008.        && EQ (last_arrow_position, Voverlay_arrow_position)
  1009.        && EQ (last_arrow_string, Voverlay_arrow_string)
  1010.        && (tem = try_window_id (FRAME_SELECTED_WINDOW (f)))
  1011.        && tem != -2)
  1012.     {
  1013.       /* tem > 0 means success.  tem == -1 means choose new start.
  1014.      tem == -2 means try again with same start,
  1015.       and nothing but whitespace follows the changed stuff.
  1016.      tem == 0 means try again with same start.  */
  1017.       if (tem > 0)
  1018.     goto done;
  1019.     }
  1020.   else if (startp >= BEGV && startp <= ZV
  1021.        /* Avoid starting display at end of buffer! */
  1022.        && (startp < ZV || startp == BEGV
  1023.            || (XFASTINT (w->last_modified) >= MODIFF)))
  1024.     {
  1025.       /* Try to redisplay starting at same place as before */
  1026.       /* If point has not moved off frame, accept the results */
  1027.       try_window (window, startp);
  1028.       if (cursor_vpos >= 0)
  1029.     {
  1030.       if (!just_this_one || clip_changed || beg_unchanged < startp)
  1031.         /* Forget any recorded base line for line number display.  */
  1032.         w->base_line_number = Qnil;
  1033.       goto done;
  1034.     }
  1035.       else
  1036.     cancel_my_columns (w);
  1037.     }
  1038.  
  1039.   XFASTINT (w->last_modified) = 0;
  1040.   w->update_mode_line = Qt;
  1041.  
  1042.   /* Try to scroll by specified few lines */
  1043.  
  1044.   if (scroll_step && !clip_changed)
  1045.     {
  1046.       if (point > startp)
  1047.     {
  1048.       pos = *vmotion (Z - XFASTINT (w->window_end_pos),
  1049.               scroll_step, width, hscroll, window);
  1050.       if (pos.vpos >= height)
  1051.         goto scroll_fail;
  1052.     }
  1053.  
  1054.       pos = *vmotion (startp, point < startp ? - scroll_step : scroll_step,
  1055.               width, hscroll, window);
  1056.  
  1057.       if (point >= pos.bufpos)
  1058.     {
  1059.       try_window (window, pos.bufpos);
  1060.       if (cursor_vpos >= 0)
  1061.         {
  1062.           if (!just_this_one || clip_changed || beg_unchanged < startp)
  1063.         /* Forget any recorded base line for line number display.  */
  1064.         w->base_line_number = Qnil;
  1065.           goto done;
  1066.         }
  1067.       else
  1068.         cancel_my_columns (w);
  1069.     }
  1070.     scroll_fail: ;
  1071.     }
  1072.  
  1073.   /* Finally, just choose place to start which centers point */
  1074.  
  1075. recenter:
  1076.   /* Forget any previously recorded base line for line number display.  */
  1077.   w->base_line_number = Qnil;
  1078.  
  1079.   pos = *vmotion (point, - height / 2, width, hscroll, window);
  1080.   try_window (window, pos.bufpos);
  1081.  
  1082.   startp = marker_position (w->start);
  1083.   w->start_at_line_beg = 
  1084.     (startp == BEGV || FETCH_CHAR (startp - 1) == '\n') ? Qt : Qnil;
  1085.  
  1086. done:
  1087.   if ((!NILP (w->update_mode_line)
  1088.        /* If window not full width, must redo its mode line
  1089.       if the window to its side is being redone */
  1090.        || (!just_this_one && width < FRAME_WIDTH (f) - 1)
  1091.        || INTEGERP (w->base_line_pos))
  1092.       && height != XFASTINT (w->height))
  1093.     display_mode_line (w);
  1094.   if (! line_number_displayed
  1095.       && ! BUFFERP (w->base_line_pos))
  1096.     {
  1097.       w->base_line_pos = Qnil;
  1098.       w->base_line_number = Qnil;
  1099.     }
  1100.  
  1101.   /* When we reach a frame's selected window, redo the frame's menu bar.  */
  1102.   if (!NILP (w->update_mode_line)
  1103.       && FRAME_MENU_BAR_LINES (f) > 0
  1104.       && EQ (FRAME_SELECTED_WINDOW (f), window))
  1105.     display_menu_bar (w);
  1106.  
  1107.  finish_scroll_bars:
  1108.   if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
  1109.     {
  1110.       int start, end, whole;
  1111.  
  1112.       /* Calculate the start and end positions for the current window.
  1113.      At some point, it would be nice to choose between scrollbars
  1114.      which reflect the whole buffer size, with special markers
  1115.      indicating narrowing, and scrollbars which reflect only the
  1116.      visible region.
  1117.  
  1118.      Note that minibuffers sometimes aren't displaying any text.  */
  1119.       if (! MINI_WINDOW_P (w)
  1120.       || (w == XWINDOW (minibuf_window) && ! echo_area_glyphs))
  1121.     {
  1122.       whole = ZV - BEGV;
  1123.       start = startp - BEGV;
  1124.       /* I don't think this is guaranteed to be right.  For the
  1125.          moment, we'll pretend it is.  */
  1126.       end = (Z - XINT (w->window_end_pos)) - BEGV;
  1127.  
  1128.       if (end < start) end = start;
  1129.       if (whole < (end - start)) whole = end - start;
  1130.     }
  1131.       else
  1132.     start = end = whole = 0;
  1133.  
  1134.       /* Indicate what this scroll bar ought to be displaying now.  */
  1135.       (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
  1136.  
  1137.       /* Note that we actually used the scroll bar attached to this window,
  1138.      so it shouldn't be deleted at the end of redisplay.  */
  1139.       (*redeem_scroll_bar_hook) (w);
  1140.     }
  1141.  
  1142.   SET_PT (opoint);
  1143.   current_buffer = old;
  1144.   SET_PT (lpoint);
  1145. }
  1146.  
  1147. /* Do full redisplay on one window, starting at position `pos'. */
  1148.  
  1149. static void
  1150. try_window (window, pos)
  1151.      Lisp_Object window;
  1152.      register int pos;
  1153. {
  1154.   register struct window *w = XWINDOW (window);
  1155.   register int height = window_internal_height (w);
  1156.   register int vpos = XFASTINT (w->top);
  1157.   register int last_text_vpos = vpos;
  1158.   int tab_offset = pos_tab_offset (w, pos);
  1159.   FRAME_PTR f = XFRAME (w->frame);
  1160.   int width = window_internal_width (w) - 1;
  1161.   struct position val;
  1162.  
  1163.   Fset_marker (w->start, make_number (pos), Qnil);
  1164.   cursor_vpos = -1;
  1165.   overlay_arrow_seen = 0;
  1166.   val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
  1167.  
  1168.   while (--height >= 0)
  1169.     {
  1170.       val = *display_text_line (w, pos, vpos, val.hpos, tab_offset);
  1171.       tab_offset += width;
  1172.       if (val.vpos) tab_offset = 0;
  1173.       vpos++;
  1174.       if (pos != val.bufpos)
  1175.     last_text_vpos
  1176.       /* Next line, unless prev line ended in end of buffer with no cr */
  1177.       = vpos - (val.vpos && FETCH_CHAR (val.bufpos - 1) != '\n');
  1178.       pos = val.bufpos;
  1179.     }
  1180.  
  1181.   /* If last line is continued in middle of character,
  1182.      include the split character in the text considered on the frame */
  1183.   if (val.hpos < (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
  1184.     pos++;
  1185.  
  1186.   /* If bottom just moved off end of frame, change mode line percentage.  */
  1187.   if (XFASTINT (w->window_end_pos) == 0
  1188.       && Z != pos)
  1189.     w->update_mode_line = Qt;
  1190.  
  1191.   /* Say where last char on frame will be, once redisplay is finished.  */
  1192.   XFASTINT (w->window_end_pos) = Z - pos;
  1193.   XFASTINT (w->window_end_vpos) = last_text_vpos - XFASTINT (w->top);
  1194.   /* But that is not valid info until redisplay finishes.  */
  1195.   w->window_end_valid = Qnil;
  1196. }
  1197.  
  1198. /* Try to redisplay when buffer is modified locally,
  1199.  computing insert/delete line to preserve text outside
  1200.  the bounds of the changes.
  1201.  Return 1 if successful, 0 if if cannot tell what to do,
  1202.  or -1 to tell caller to find a new window start,
  1203.  or -2 to tell caller to do normal redisplay with same window start.  */
  1204.  
  1205. static int
  1206. try_window_id (window)
  1207.      Lisp_Object window;
  1208. {
  1209.   int pos;
  1210.   register struct window *w = XWINDOW (window);
  1211.   register int height = window_internal_height (w);
  1212.   FRAME_PTR f = XFRAME (w->frame);
  1213.   int top = XFASTINT (w->top);
  1214.   int start = marker_position (w->start);
  1215.   int width = window_internal_width (w) - 1;
  1216.   int hscroll = XINT (w->hscroll);
  1217.   int lmargin = hscroll > 0 ? 1 - hscroll : 0;
  1218.   register int vpos;
  1219.   register int i, tem;
  1220.   int last_text_vpos = 0;
  1221.   int stop_vpos;
  1222.  
  1223.   struct position val, bp, ep, xp, pp;
  1224.   int scroll_amount = 0;
  1225.   int delta;
  1226.   int tab_offset, epto;
  1227.  
  1228.   if (GPT - BEG < beg_unchanged)
  1229.     beg_unchanged = GPT - BEG;
  1230.   if (Z - GPT < end_unchanged)
  1231.     end_unchanged = Z - GPT;
  1232.  
  1233.   if (beg_unchanged + 1 < start)
  1234.     return 0;            /* Give up if changes go above top of window */
  1235.  
  1236.   /* Find position before which nothing is changed.  */
  1237.   bp = *compute_motion (start, 0, lmargin,
  1238.             beg_unchanged + 1, height + 1, 0, width, hscroll,
  1239.             pos_tab_offset (w, start));
  1240.   if (bp.vpos >= height)
  1241.     {
  1242.       if (point < bp.bufpos && !bp.contin)
  1243.     {
  1244.       /* All changes are below the frame, and point is on the frame.
  1245.          We don't need to change the frame at all.
  1246.          But we need to update window_end_pos to account for
  1247.          any change in buffer size.  */
  1248.       bp = *compute_motion (start, 0, lmargin,
  1249.                 Z, height, 0,
  1250.                 width, hscroll, pos_tab_offset (w, start));
  1251.       XFASTINT (w->window_end_vpos) = height;
  1252.       XFASTINT (w->window_end_pos) = Z - bp.bufpos;
  1253.       return 1;
  1254.     }
  1255.       return 0;
  1256.     }
  1257.  
  1258.   vpos = bp.vpos;
  1259.  
  1260.   /* Find beginning of that frame line.  Must display from there.  */
  1261.   bp = *vmotion (bp.bufpos, 0, width, hscroll, window);
  1262.  
  1263.   pos = bp.bufpos;
  1264.   val.hpos = lmargin;
  1265.   if (pos < start)
  1266.     return -1;
  1267.  
  1268.   /* If about to start displaying at the beginning of a continuation line,
  1269.      really start with previous frame line, in case it was not
  1270.      continued when last redisplayed */
  1271.   if ((bp.contin && bp.bufpos - 1 == beg_unchanged && vpos > 0)
  1272.       ||
  1273.       /* Likewise if we have to worry about selective display.  */
  1274.       (XTYPE (current_buffer->selective_display) == Lisp_Int
  1275.        && XINT (current_buffer->selective_display) > 0
  1276.        && bp.bufpos - 1 == beg_unchanged && vpos > 0))
  1277.     {
  1278.       bp = *vmotion (bp.bufpos, -1, width, hscroll, window);
  1279.       --vpos;
  1280.       pos = bp.bufpos;
  1281.     }
  1282.  
  1283.   if (bp.contin && bp.hpos != lmargin)
  1284.     {
  1285.       val.hpos = bp.prevhpos - width + lmargin;
  1286.       pos--;
  1287.     }
  1288.  
  1289.   bp.vpos = vpos;
  1290.  
  1291.   /* Find first visible newline after which no more is changed.  */
  1292.   tem = find_next_newline (Z - max (end_unchanged, Z - ZV), 1);
  1293.   if (XTYPE (current_buffer->selective_display) == Lisp_Int
  1294.       && XINT (current_buffer->selective_display) > 0)
  1295.     while (tem < ZV - 1
  1296.        && (position_indentation (tem)
  1297.            >= XINT (current_buffer->selective_display)))
  1298.       tem = find_next_newline (tem, 1);
  1299.  
  1300.   /* Compute the cursor position after that newline.  */
  1301.   ep = *compute_motion (pos, vpos, val.hpos, tem,
  1302.             height, - (1 << (SHORTBITS - 1)),
  1303.             width, hscroll, pos_tab_offset (w, bp.bufpos));
  1304.  
  1305.   /* If changes reach past the text available on the frame,
  1306.      just display rest of frame.  */
  1307.   if (ep.bufpos > Z - XFASTINT (w->window_end_pos))
  1308.     stop_vpos = height;
  1309.   else
  1310.     stop_vpos = ep.vpos;
  1311.  
  1312.   /* If no newline before ep, the line ep is on includes some changes
  1313.      that must be displayed.  Make sure we don't stop before it.  */
  1314.   /* Also, if changes reach all the way until ep.bufpos,
  1315.      it is possible that something was deleted after the
  1316.      newline before it, so the following line must be redrawn. */
  1317.   if (stop_vpos == ep.vpos
  1318.       && (ep.bufpos == BEGV
  1319.       || FETCH_CHAR (ep.bufpos - 1) != '\n'
  1320.       || ep.bufpos == Z - end_unchanged))
  1321.     stop_vpos = ep.vpos + 1;
  1322.  
  1323.   cursor_vpos = -1;
  1324.   overlay_arrow_seen = 0;
  1325.  
  1326.   /* If changes do not reach to bottom of window,
  1327.      figure out how much to scroll the rest of the window */
  1328.   if (stop_vpos < height)
  1329.     {
  1330.       /* Now determine how far up or down the rest of the window has moved */
  1331.       epto = pos_tab_offset (w, ep.bufpos);
  1332.       xp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos,
  1333.                 Z - XFASTINT (w->window_end_pos),
  1334.                 10000, 0, width, hscroll, epto);
  1335.       scroll_amount = xp.vpos - XFASTINT (w->window_end_vpos);
  1336.  
  1337.       /* Is everything on frame below the changes whitespace?
  1338.      If so, no scrolling is really necessary.  */
  1339.       for (i = ep.bufpos; i < xp.bufpos; i++)
  1340.     {
  1341.       tem = FETCH_CHAR (i);
  1342.       if (tem != ' ' && tem != '\n' && tem != '\t')
  1343.         break;
  1344.     }
  1345.       if (i == xp.bufpos)
  1346.     return -2;
  1347.  
  1348.       XFASTINT (w->window_end_vpos) += scroll_amount;
  1349.  
  1350.       /* Before doing any scrolling, verify that point will be on frame. */
  1351.       if (point > ep.bufpos && !(point <= xp.bufpos && xp.bufpos < height))
  1352.     {
  1353.       if (point <= xp.bufpos)
  1354.         {
  1355.           pp = *compute_motion (ep.bufpos, ep.vpos, ep.hpos,
  1356.                     point, height, - (1 << (SHORTBITS - 1)),
  1357.                     width, hscroll, epto);
  1358.         }
  1359.       else
  1360.         {
  1361.           pp = *compute_motion (xp.bufpos, xp.vpos, xp.hpos,
  1362.                     point, height, - (1 << (SHORTBITS - 1)),
  1363.                     width, hscroll, pos_tab_offset (w, xp.bufpos));
  1364.         }
  1365.       if (pp.bufpos < point || pp.vpos == height)
  1366.         return 0;
  1367.       cursor_vpos = pp.vpos + top;
  1368.       cursor_hpos = pp.hpos + XFASTINT (w->left);
  1369.     }
  1370.  
  1371.       if (stop_vpos - scroll_amount >= height
  1372.       || ep.bufpos == xp.bufpos)
  1373.     {
  1374.       if (scroll_amount < 0)
  1375.         stop_vpos -= scroll_amount;
  1376.       scroll_amount = 0;
  1377.       /* In this path, we have altered window_end_vpos
  1378.          and not left it negative.
  1379.          We must make sure that, in case display is preempted
  1380.          before the frame changes to reflect what we do here,
  1381.          further updates will not come to try_window_id
  1382.          and assume the frame and window_end_vpos match.  */
  1383.       blank_end_of_window = 1;
  1384.     }
  1385.       else if (!scroll_amount)
  1386.     {}
  1387.       else if (bp.bufpos == Z - end_unchanged)
  1388.     {
  1389.       /* If reprinting everything is nearly as fast as scrolling,
  1390.          don't bother scrolling.  Can happen if lines are short.  */
  1391.       if (scroll_cost (f, bp.vpos + top - scroll_amount,
  1392.                top + height - max (0, scroll_amount),
  1393.                scroll_amount)
  1394.           > xp.bufpos - bp.bufpos - 20)
  1395.         /* Return "try normal display with same window-start."
  1396.            Too bad we can't prevent further scroll-thinking.  */
  1397.         return -2;
  1398.       /* If pure deletion, scroll up as many lines as possible.
  1399.          In common case of killing a line, this can save the
  1400.          following line from being overwritten by scrolling
  1401.          and therefore having to be redrawn.  */
  1402.       tem = scroll_frame_lines (f, bp.vpos + top - scroll_amount,
  1403.                      top + height - max (0, scroll_amount),
  1404.                      scroll_amount);
  1405.       if (!tem) stop_vpos = height;
  1406.     }
  1407.       else if (scroll_amount)
  1408.     {
  1409.       /* If reprinting everything is nearly as fast as scrolling,
  1410.          don't bother scrolling.  Can happen if lines are short.  */
  1411.       /* Note that if scroll_amount > 0, xp.bufpos - bp.bufpos is an
  1412.          overestimate of cost of reprinting, since xp.bufpos
  1413.          would end up below the bottom of the window.  */
  1414.       if (scroll_cost (f, ep.vpos + top - scroll_amount,
  1415.                top + height - max (0, scroll_amount),
  1416.                scroll_amount)
  1417.           > xp.bufpos - ep.bufpos - 20)
  1418.         /* Return "try normal display with same window-start."
  1419.            Too bad we can't prevent further scroll-thinking.  */
  1420.         return -2;
  1421.       tem = scroll_frame_lines (f, ep.vpos + top - scroll_amount,
  1422.                      top + height - max (0, scroll_amount),
  1423.                      scroll_amount);
  1424.       if (!tem) stop_vpos = height;
  1425.     }
  1426.     }
  1427.  
  1428.   /* In any case, do not display past bottom of window */
  1429.   if (stop_vpos >= height)
  1430.     {
  1431.       stop_vpos = height;
  1432.       scroll_amount = 0;
  1433.     }
  1434.  
  1435.   /* Handle case where pos is before w->start --
  1436.      can happen if part of line had been clipped and is not clipped now */
  1437.   if (vpos == 0 && pos < marker_position (w->start))
  1438.     Fset_marker (w->start, make_number (pos), Qnil);
  1439.  
  1440.   /* Redisplay the lines where the text was changed */
  1441.   last_text_vpos = vpos;
  1442.   tab_offset = pos_tab_offset (w, pos);
  1443.   /* If we are starting display in mid-character, correct tab_offset
  1444.      to account for passing the line that that character really starts in.  */
  1445.   if (val.hpos < lmargin)
  1446.     tab_offset += width;
  1447.   while (vpos < stop_vpos)
  1448.     {
  1449.       val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
  1450.       tab_offset += width;
  1451.       if (val.vpos) tab_offset = 0;
  1452.       if (pos != val.bufpos)
  1453.     last_text_vpos
  1454.       /* Next line, unless prev line ended in end of buffer with no cr */
  1455.         = vpos - (val.vpos && FETCH_CHAR (val.bufpos - 1) != '\n');
  1456.       pos = val.bufpos;
  1457.     }
  1458.  
  1459.   /* There are two cases:
  1460.      1) we have displayed down to the bottom of the window
  1461.      2) we have scrolled lines below stop_vpos by scroll_amount  */
  1462.  
  1463.   if (vpos == height)
  1464.     {
  1465.       /* If last line is continued in middle of character,
  1466.      include the split character in the text considered on the frame */
  1467.       if (val.hpos < lmargin)
  1468.     val.bufpos++;
  1469.       XFASTINT (w->window_end_vpos) = last_text_vpos;
  1470.       XFASTINT (w->window_end_pos) = Z - val.bufpos;
  1471.     }
  1472.  
  1473.   /* If scrolling made blank lines at window bottom,
  1474.      redisplay to fill those lines */
  1475.   if (scroll_amount < 0)
  1476.     {
  1477.       /* Don't consider these lines for general-purpose scrolling.
  1478.      That will save time in the scrolling computation.  */
  1479.       FRAME_SCROLL_BOTTOM_VPOS (f) = xp.vpos;
  1480.       vpos = xp.vpos;
  1481.       pos = xp.bufpos;
  1482.       val.hpos = lmargin;
  1483.       if (pos == ZV)
  1484.     vpos = height + scroll_amount;
  1485.       else if (xp.contin && xp.hpos != lmargin)
  1486.     {
  1487.       val.hpos = xp.prevhpos - width + lmargin;
  1488.       pos--;
  1489.     }
  1490.  
  1491.       blank_end_of_window = 1;
  1492.       tab_offset = pos_tab_offset (w, pos);
  1493.       /* If we are starting display in mid-character, correct tab_offset
  1494.      to account for passing the line that that character starts in.  */
  1495.       if (val.hpos < lmargin)
  1496.     tab_offset += width;
  1497.  
  1498.       while (vpos < height)
  1499.     {
  1500.       val = *display_text_line (w, pos, top + vpos++, val.hpos, tab_offset);
  1501.       tab_offset += width;
  1502.       if (val.vpos) tab_offset = 0;
  1503.       pos = val.bufpos;
  1504.     }
  1505.  
  1506.       /* Here is a case where display_text_line sets cursor_vpos wrong.
  1507.      Make it be fixed up, below.  */
  1508.       if (xp.bufpos == ZV
  1509.       && xp.bufpos == point)
  1510.     cursor_vpos = -1;
  1511.     }
  1512.  
  1513.   /* If bottom just moved off end of frame, change mode line percentage.  */
  1514.   if (XFASTINT (w->window_end_pos) == 0
  1515.       && Z != val.bufpos)
  1516.     w->update_mode_line = Qt;
  1517.  
  1518.   /* Attempt to adjust end-of-text positions to new bottom line */
  1519.   if (scroll_amount)
  1520.     {
  1521.       delta = height - xp.vpos;
  1522.       if (delta < 0
  1523.       || (delta > 0 && xp.bufpos <= ZV)
  1524.       || (delta == 0 && xp.hpos))
  1525.     {
  1526.       val = *vmotion (Z - XFASTINT (w->window_end_pos),
  1527.               delta, width, hscroll, window);
  1528.       XFASTINT (w->window_end_pos) = Z - val.bufpos;
  1529.       XFASTINT (w->window_end_vpos) += val.vpos;
  1530.     }
  1531.     }
  1532.  
  1533.   w->window_end_valid = Qnil;
  1534.  
  1535.   /* If point was not in a line that was displayed, find it */
  1536.   if (cursor_vpos < 0)
  1537.     {
  1538.       val = *compute_motion (start, 0, lmargin, point, 10000, 10000,
  1539.                  width, hscroll, pos_tab_offset (w, start));
  1540.       /* Admit failure if point is off frame now */
  1541.       if (val.vpos >= height)
  1542.     {
  1543.       for (vpos = 0; vpos < height; vpos++)
  1544.         cancel_line (vpos + top, f);
  1545.       return 0;
  1546.     }
  1547.       cursor_vpos = val.vpos + top;
  1548.       cursor_hpos = val.hpos + XFASTINT (w->left);
  1549.     }
  1550.  
  1551.   FRAME_CURSOR_X (f) = max (0, cursor_hpos);
  1552.   FRAME_CURSOR_Y (f) = cursor_vpos;
  1553.  
  1554.   if (debug_end_pos)
  1555.     {
  1556.       val = *compute_motion (start, 0, lmargin, ZV,
  1557.                  height, - (1 << (SHORTBITS - 1)),
  1558.                  width, hscroll, pos_tab_offset (w, start));
  1559.       if (val.vpos != XFASTINT (w->window_end_vpos))
  1560.     abort ();
  1561.       if (XFASTINT (w->window_end_pos)
  1562.       != Z - val.bufpos)
  1563.     abort ();
  1564.     }
  1565.  
  1566.   return 1;
  1567. }
  1568.  
  1569. /* Mark a section of BUF as modified, but only for the sake of redisplay.
  1570.    This is useful for recording changes to overlays.
  1571.  
  1572.    We increment the buffer's modification timestamp and set the
  1573.    redisplay caches (windows_or_buffers_changed, beg_unchanged, etc)
  1574.    as if the region of text between START and END had been modified;
  1575.    the redisplay code will check this against the windows' timestamps,
  1576.    and redraw the appropriate area of the buffer.
  1577.  
  1578.    However, if the buffer is unmodified, we bump the last-save
  1579.    timestamp as well, so that incrementing the timestamp doesn't fool
  1580.    Emacs into thinking that the buffer's text has been modified. 
  1581.  
  1582.    Tweaking the timestamps shouldn't hurt the first-modification
  1583.    timestamps recorded in the undo records; those values aren't
  1584.    written until just before a real text modification is made, so they
  1585.    will never catch the timestamp value just before this function gets
  1586.    called.  */
  1587.  
  1588. void
  1589. redisplay_region (buf, start, end)
  1590.      struct buffer *buf;
  1591.      int start, end;
  1592. {
  1593.   if (start == end)
  1594.     return;
  1595.  
  1596.   if (start > end)
  1597.     {
  1598.       int temp = start;
  1599.       start = end; end = temp;
  1600.     }
  1601.  
  1602.   if (buf != current_buffer)
  1603.     windows_or_buffers_changed = 1;
  1604.   else
  1605.     {
  1606.       if (unchanged_modified == MODIFF)
  1607.     {
  1608.       beg_unchanged = start - BEG;
  1609.       end_unchanged = Z - end;
  1610.     }
  1611.       else
  1612.     {
  1613.       if (Z - end < end_unchanged)
  1614.         end_unchanged = Z - end;
  1615.       if (start - BEG < beg_unchanged)
  1616.         beg_unchanged = start - BEG;
  1617.     }
  1618.     }
  1619.  
  1620.   /* Increment the buffer's time stamp, but also increment the save
  1621.      and autosave timestamps, so as not to screw up that timekeeping. */
  1622.   if (BUF_MODIFF (buf) == buf->save_modified)
  1623.     buf->save_modified++;
  1624.   if (BUF_MODIFF (buf) == buf->auto_save_modified)
  1625.     buf->auto_save_modified++;
  1626.  
  1627.   BUF_MODIFF (buf) ++;
  1628. }
  1629.  
  1630.  
  1631. /* Copy glyphs from the vector FROM to the rope T.
  1632.    But don't actually copy the parts that would come in before S.
  1633.    Value is T, advanced past the copied data.  */
  1634.  
  1635. GLYPH *
  1636. copy_rope (t, s, from, face)
  1637.      register GLYPH *t; /* Copy to here. */
  1638.      register GLYPH *s; /* Starting point. */
  1639.      Lisp_Object from;  /* Data to copy; known to be a vector.  */
  1640.      int face;        /* Face to apply to glyphs which don't specify one. */
  1641. {
  1642.   register int n = XVECTOR (from)->size;
  1643.   register Lisp_Object *f = XVECTOR (from)->contents;
  1644.  
  1645.   while (n--)
  1646.     {
  1647.       int glyph = XFASTINT (*f);
  1648.  
  1649.       if (t >= s) *t = MAKE_GLYPH (GLYPH_CHAR (glyph),
  1650.                    (GLYPH_FACE (glyph)
  1651.                     ? GLYPH_FACE (glyph)
  1652.                     : face));
  1653.       ++t;
  1654.       ++f;
  1655.     }
  1656.   return t;
  1657. }
  1658.  
  1659. /* Copy exactly LEN glyphs from FROM into data at T.
  1660.    But don't alter words before S.  */
  1661.  
  1662. GLYPH *
  1663. copy_part_of_rope (t, s, from, len, face)
  1664.      register GLYPH *t; /* Copy to here. */
  1665.      register GLYPH *s; /* Starting point. */
  1666.      Lisp_Object *from;  /* Data to copy. */
  1667.      int len;
  1668.      int face;        /* Face to apply to glyphs which don't specify one. */
  1669. {
  1670.   int n = len;
  1671.   register Lisp_Object *f = from;
  1672.  
  1673.   while (n--)
  1674.     {
  1675.       int glyph = XFASTINT (*f);
  1676.  
  1677.       if (t >= s) *t = MAKE_GLYPH (GLYPH_CHAR (glyph),
  1678.                    (GLYPH_FACE (glyph)
  1679.                     ? GLYPH_FACE (glyph)
  1680.                     : face));
  1681.       ++t;
  1682.       ++f;
  1683.     }
  1684.   return t;
  1685. }
  1686.  
  1687. /* Display one line of window w, starting at position START in W's buffer.
  1688.    Display starting at horizontal position HPOS, which is normally zero
  1689.    or negative.  A negative value causes output up to hpos = 0 to be discarded.
  1690.    This is done for negative hscroll, or when this is a continuation line
  1691.    and the continuation occurred in the middle of a multi-column character.
  1692.  
  1693.    TABOFFSET is an offset for ostensible hpos, used in tab stop calculations.
  1694.  
  1695.    Display on position VPOS on the frame.  (origin 0).
  1696.  
  1697.    Returns a STRUCT POSITION giving character to start next line with
  1698.    and where to display it, including a zero or negative hpos.
  1699.    The vpos field is not really a vpos; it is 1 unless the line is continued */
  1700.  
  1701. struct position val_display_text_line;
  1702.  
  1703. static struct position *
  1704. display_text_line (w, start, vpos, hpos, taboffset)
  1705.      struct window *w;
  1706.      int start;
  1707.      int vpos;
  1708.      int hpos;
  1709.      int taboffset;
  1710. {
  1711.   register int pos = start;
  1712.   register int c;
  1713.   register GLYPH *p1;
  1714.   int end;
  1715.   register int pause;
  1716.   register unsigned char *p;
  1717.   GLYPH *endp;
  1718.   register GLYPH *startp;
  1719.   register GLYPH *p1prev = 0;
  1720.   FRAME_PTR f = XFRAME (w->frame);
  1721.   int tab_width = XINT (current_buffer->tab_width);
  1722.   int ctl_arrow = !NILP (current_buffer->ctl_arrow);
  1723.   int width = window_internal_width (w) - 1;
  1724.   struct position val;
  1725.   int lastpos;
  1726.   int invis;
  1727.   int hscroll = XINT (w->hscroll);
  1728.   int truncate = hscroll
  1729.     || (truncate_partial_width_windows
  1730.     && XFASTINT (w->width) < FRAME_WIDTH (f))
  1731.     || !NILP (current_buffer->truncate_lines);
  1732.  
  1733.   /* 1 if we should highlight the region.  */
  1734.   int highlight_region
  1735.     = !NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active);
  1736.   int region_beg, region_end;
  1737.  
  1738.   int selective
  1739.     = XTYPE (current_buffer->selective_display) == Lisp_Int
  1740.       ? XINT (current_buffer->selective_display)
  1741.     : !NILP (current_buffer->selective_display) ? -1 : 0;
  1742.   register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
  1743.   register struct Lisp_Vector *dp = window_display_table (w);
  1744.  
  1745.   Lisp_Object default_invis_vector[3];
  1746.   /* Nonzero means display something where there are invisible lines.
  1747.      The precise value is the number of glyphs to display.  */
  1748.   int selective_rlen
  1749.     = (selective && dp && XTYPE (DISP_INVIS_VECTOR (dp)) == Lisp_Vector
  1750.        ? XVECTOR (DISP_INVIS_VECTOR (dp))->size
  1751.        : selective && !NILP (current_buffer->selective_display_ellipses)
  1752.        ? 3 : 0);
  1753.   /* This is the sequence of Lisp objects to display
  1754.      when there are invisible lines.  */
  1755.   Lisp_Object *invis_vector_contents
  1756.     = (dp && XTYPE (DISP_INVIS_VECTOR (dp)) == Lisp_Vector
  1757.        ? XVECTOR (DISP_INVIS_VECTOR (dp))->contents
  1758.        : default_invis_vector);
  1759.  
  1760.   GLYPH truncator = (dp == 0 || XTYPE (DISP_TRUNC_GLYPH (dp)) != Lisp_Int
  1761.             ? '$' : XINT (DISP_TRUNC_GLYPH (dp)));
  1762.   GLYPH continuer = (dp == 0 || XTYPE (DISP_CONTINUE_GLYPH (dp)) != Lisp_Int
  1763.             ? '\\' : XINT (DISP_CONTINUE_GLYPH (dp)));
  1764.  
  1765.   /* The next buffer location at which the face should change, due
  1766.      to overlays or text property changes.  */
  1767.   int next_face_change;
  1768.  
  1769.   /* The face we're currently using.  */
  1770.   int current_face = 0;
  1771.  
  1772.   XFASTINT (default_invis_vector[2]) = '.';
  1773.   default_invis_vector[0] = default_invis_vector[1] = default_invis_vector[2];
  1774.  
  1775.   hpos += XFASTINT (w->left);
  1776.   get_display_line (f, vpos, XFASTINT (w->left));
  1777.   if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
  1778.  
  1779.   /* Show where to highlight the region.  */
  1780.   if (highlight_region && XMARKER (current_buffer->mark)->buffer != 0
  1781.       /* Maybe highlight only in selected window.  */
  1782.       && (highlight_nonselected_windows
  1783.       || w == XWINDOW (selected_window)))
  1784.     {
  1785.       region_beg = marker_position (current_buffer->mark);
  1786.       if (PT < region_beg)
  1787.     {
  1788.       region_end = region_beg;
  1789.       region_beg = PT;
  1790.     }
  1791.       else
  1792.     region_end = PT;
  1793.       w->region_showing = Qt;
  1794.     }
  1795.   else
  1796.     region_beg = region_end = -1;
  1797.  
  1798.   if (MINI_WINDOW_P (w) && start == 1
  1799.       && vpos == XFASTINT (w->top))
  1800.     {
  1801.       if (minibuf_prompt)
  1802.     hpos = display_string (w, vpos, minibuf_prompt, hpos,
  1803.                    (!truncate ? continuer : truncator),
  1804.                    -1, -1);
  1805.       minibuf_prompt_width = hpos;
  1806.     }
  1807.  
  1808.   desired_glyphs->bufp[vpos] = pos;
  1809.   p1 = desired_glyphs->glyphs[vpos] + hpos;
  1810.   end = ZV;
  1811.   startp = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
  1812.   endp = startp + width;
  1813.  
  1814.   /* Arrange the overlays nicely for our purposes.  Usually, we call
  1815.      display_text_line on only one line at a time, in which case this
  1816.      can't really hurt too much, or we call it on lines which appear
  1817.      one after another in the buffer, in which case all calls to
  1818.      recenter_overlay_lists but the first will be pretty cheap.  */
  1819.   recenter_overlay_lists (current_buffer, pos);
  1820.  
  1821.   /* Loop generating characters.
  1822.      Stop at end of buffer, before newline,
  1823.      if reach or pass continuation column,
  1824.      or at face change.  */
  1825.   pause = pos;
  1826.   next_face_change = pos;
  1827.   while (p1 < endp)
  1828.     {
  1829.       p1prev = p1;
  1830.       if (pos >= pause)
  1831.     {
  1832.       /* Did we hit the end of the visible region of the buffer?
  1833.          Stop here.  */
  1834.       if (pos >= end)
  1835.         break;
  1836.  
  1837.       /* Did we reach point?  Record the cursor location.  */
  1838.       if (pos == point && cursor_vpos < 0)
  1839.         {
  1840.           cursor_vpos = vpos;
  1841.           cursor_hpos = p1 - startp;
  1842.         }
  1843.  
  1844. #ifdef HAVE_X_WINDOWS
  1845.       /* Did we hit a face change?  Figure out what face we should
  1846.          use now.  We also hit this the first time through the
  1847.          loop, to see what face we should start with.  */
  1848.       if (pos == next_face_change && FRAME_X_P (f))
  1849.         current_face = compute_char_face (f, w, pos,
  1850.                           region_beg, region_end,
  1851.                           &next_face_change);
  1852. #endif
  1853.  
  1854.       pause = end;
  1855.  
  1856.       if (pos < next_face_change && next_face_change < pause)
  1857.         pause = next_face_change;
  1858.  
  1859.       /* Wouldn't you hate to read the next line to someone over
  1860.              the phone?  */
  1861.       if (pos < point && point < pause)
  1862.         pause = point;
  1863.       if (pos < GPT && GPT < pause)
  1864.         pause = GPT;
  1865.  
  1866.       p = &FETCH_CHAR (pos);
  1867.     }
  1868.       c = *p++;
  1869.       if (c >= 040 && c < 0177
  1870.       && (dp == 0 || XTYPE (DISP_CHAR_VECTOR (dp, c)) != Lisp_Vector))
  1871.     {
  1872.       if (p1 >= startp)
  1873.         *p1 = MAKE_GLYPH (c, current_face);
  1874.       p1++;
  1875.     }
  1876.       else if (c == '\n')
  1877.     {
  1878.       invis = 0;
  1879.       while (pos < end
  1880.          && selective > 0
  1881.          && position_indentation (pos + 1) >= selective)
  1882.         {
  1883.           invis = 1;
  1884.           pos = find_next_newline (pos + 1, 1);
  1885.           if (FETCH_CHAR (pos - 1) == '\n')
  1886.         pos--;
  1887.         }
  1888.       if (invis && selective_rlen > 0 && p1 >= startp)
  1889.         {
  1890.           p1 += selective_rlen;
  1891.           if (p1 - startp > width)
  1892.         p1 = endp;
  1893.           copy_part_of_rope (p1prev, p1prev, invis_vector_contents,
  1894.                  (p1 - p1prev), current_face);
  1895.         }
  1896. #if 1
  1897.       /* Draw the face of the newline character as extending all the 
  1898.          way to the end of the frame line.  */
  1899.       if (current_face)
  1900.         while (p1 < endp)
  1901.           *p1++ = MAKE_GLYPH (' ', current_face);
  1902. #endif
  1903.       break;
  1904.     }
  1905.       else if (c == '\t')
  1906.     {
  1907.       do
  1908.         {
  1909.           if (p1 >= startp && p1 < endp)
  1910.         *p1 = MAKE_GLYPH (' ', current_face);
  1911.           p1++;
  1912.         }
  1913.       while ((p1 - startp + taboffset + hscroll - (hscroll > 0))
  1914.          % tab_width);
  1915.     }
  1916.       else if (c == Ctl ('M') && selective == -1)
  1917.     {
  1918.       pos = find_next_newline (pos, 1);
  1919.       if (FETCH_CHAR (pos - 1) == '\n')
  1920.         pos--;
  1921.       if (selective_rlen > 0)
  1922.         {
  1923.           p1 += selective_rlen;
  1924.           if (p1 - startp > width)
  1925.         p1 = endp;
  1926.           copy_part_of_rope (p1prev, p1prev, invis_vector_contents,
  1927.                  (p1 - p1prev), current_face);
  1928.         }
  1929. #if 1
  1930.       /* Draw the face of the newline character as extending all the 
  1931.          way to the end of the frame line.  */
  1932.       if (current_face)
  1933.         while (p1 < endp)
  1934.           *p1++ = MAKE_GLYPH (' ', current_face);
  1935. #endif
  1936.       break;
  1937.     }
  1938.       else if (dp != 0 && XTYPE (DISP_CHAR_VECTOR (dp, c)) == Lisp_Vector)
  1939.     {
  1940.       p1 = copy_rope (p1, startp, DISP_CHAR_VECTOR (dp, c), current_face);
  1941.     }
  1942.       else if (c < 0200 && ctl_arrow)
  1943.     {
  1944.       if (p1 >= startp)
  1945.         *p1 = MAKE_GLYPH ((dp && XTYPE (DISP_CTRL_GLYPH (dp)) == Lisp_Int
  1946.                    ? XINT (DISP_CTRL_GLYPH (dp)) : '^'),
  1947.                   current_face);
  1948.       p1++;
  1949.       if (p1 >= startp && p1 < endp)
  1950.         *p1 = MAKE_GLYPH (c ^ 0100, current_face);
  1951.       p1++;
  1952.     }
  1953.       else
  1954.     {
  1955.       if (p1 >= startp)
  1956.         *p1 = MAKE_GLYPH ((dp && XTYPE (DISP_ESCAPE_GLYPH (dp)) == Lisp_Int
  1957.                    ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\'),
  1958.                   current_face);
  1959.       p1++;
  1960.       if (p1 >= startp && p1 < endp)
  1961.         *p1 = MAKE_GLYPH ((c >> 6) + '0', current_face);
  1962.       p1++;
  1963.       if (p1 >= startp && p1 < endp)
  1964.         *p1 = MAKE_GLYPH ((7 & (c >> 3)) + '0', current_face);
  1965.       p1++;
  1966.       if (p1 >= startp && p1 < endp)
  1967.         *p1 = MAKE_GLYPH ((7 & c) + '0', current_face);
  1968.       p1++;
  1969.     }
  1970.  
  1971.       pos++;
  1972.     }
  1973.  
  1974.   val.hpos = - XINT (w->hscroll);
  1975.   if (val.hpos)
  1976.     val.hpos++;
  1977.  
  1978.   val.vpos = 1;
  1979.  
  1980.   lastpos = pos;
  1981.  
  1982.   /* Handle continuation in middle of a character */
  1983.   /* by backing up over it */
  1984.   if (p1 > endp)
  1985.     {
  1986.       /* Don't back up if we never actually displayed any text.
  1987.      This occurs when the minibuffer prompt takes up the whole line.  */
  1988.       if (p1prev)
  1989.     {
  1990.       /* Start the next line with that same character */
  1991.       pos--;
  1992.       /* but at negative hpos, to skip the columns output on this line.  */
  1993.       val.hpos += p1prev - endp;
  1994.     }
  1995.  
  1996.       /* Keep in this line everything up to the continuation column.  */
  1997.       p1 = endp;
  1998.     }
  1999.  
  2000.   /* Finish deciding which character to start the next line on,
  2001.      and what hpos to start it at.
  2002.      Also set `lastpos' to the last position which counts as "on this line"
  2003.      for cursor-positioning.  */
  2004.  
  2005.   if (pos < ZV)
  2006.     {
  2007.       if (FETCH_CHAR (pos) == '\n')
  2008.     /* If stopped due to a newline, start next line after it */
  2009.     pos++;
  2010.       else
  2011.     /* Stopped due to right margin of window */
  2012.     {
  2013.       if (truncate)
  2014.         {
  2015.           *p1++ = truncator;
  2016.           /* Truncating => start next line after next newline,
  2017.          and point is on this line if it is before the newline,
  2018.          and skip none of first char of next line */
  2019.           pos = find_next_newline (pos, 1);
  2020.           val.hpos = XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0;
  2021.  
  2022.           lastpos = pos - (FETCH_CHAR (pos - 1) == '\n');
  2023.         }
  2024.       else
  2025.         {
  2026.           *p1++ = continuer;
  2027.           val.vpos = 0;
  2028.           lastpos--;
  2029.         }
  2030.     }
  2031.     }
  2032.  
  2033.   /* If point is at eol or in invisible text at eol,
  2034.      record its frame location now.  */
  2035.  
  2036.   if (start <= point && point <= lastpos && cursor_vpos < 0)
  2037.     {
  2038.       cursor_vpos = vpos;
  2039.       cursor_hpos = p1 - startp;
  2040.     }
  2041.  
  2042.   if (cursor_vpos == vpos)
  2043.     {
  2044.       if (cursor_hpos < 0) cursor_hpos = 0;
  2045.       if (cursor_hpos > width) cursor_hpos = width;
  2046.       cursor_hpos += XFASTINT (w->left);
  2047.       if (w == XWINDOW (FRAME_SELECTED_WINDOW (f)))
  2048.     {
  2049.       FRAME_CURSOR_Y (f) = cursor_vpos;
  2050.       FRAME_CURSOR_X (f) = cursor_hpos;
  2051.  
  2052.       if (w == XWINDOW (selected_window))
  2053.         {
  2054.           /* Line is not continued and did not start
  2055.          in middle of character */
  2056.           if ((hpos - XFASTINT (w->left)
  2057.            == (XINT (w->hscroll) ? 1 - XINT (w->hscroll) : 0))
  2058.           && val.vpos)
  2059.         {
  2060.           this_line_bufpos = start;
  2061.           this_line_buffer = current_buffer;
  2062.           this_line_vpos = cursor_vpos;
  2063.           this_line_start_hpos = hpos;
  2064.           this_line_endpos = Z - lastpos;
  2065.         }
  2066.           else
  2067.         this_line_bufpos = 0;
  2068.         }
  2069.     }
  2070.     }
  2071.  
  2072.   /* If hscroll and line not empty, insert truncation-at-left marker */
  2073.   if (hscroll && lastpos != start)
  2074.     {
  2075.       *startp = truncator;
  2076.       if (p1 <= startp)
  2077.     p1 = startp + 1;
  2078.     }
  2079.  
  2080.   if (XFASTINT (w->width) + XFASTINT (w->left) != FRAME_WIDTH (f))
  2081.     {
  2082.       endp++;
  2083.       if (p1 < startp) p1 = startp;
  2084.       while (p1 < endp) *p1++ = SPACEGLYPH;
  2085.  
  2086.       /* Don't draw vertical bars if we're using scroll bars.  They're
  2087.          covered up by the scroll bars, and it's distracting to see
  2088.          them when the scroll bar windows are flickering around to be
  2089.          reconfigured.  */
  2090.       *p1++ = (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
  2091.            ? ' ' : '|');
  2092.     }
  2093.   desired_glyphs->used[vpos] = max (desired_glyphs->used[vpos],
  2094.                    p1 - desired_glyphs->glyphs[vpos]);
  2095.   desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
  2096.  
  2097.   /* If the start of this line is the overlay arrow-position,
  2098.      then put the arrow string into the display-line.  */
  2099.  
  2100.   if (XTYPE (Voverlay_arrow_position) == Lisp_Marker
  2101.       && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
  2102.       && start == marker_position (Voverlay_arrow_position)
  2103.       && XTYPE (Voverlay_arrow_string) == Lisp_String
  2104.       && ! overlay_arrow_seen)
  2105.     {
  2106.       unsigned char *p = XSTRING (Voverlay_arrow_string)->data;
  2107.       int i;
  2108.       int len = XSTRING (Voverlay_arrow_string)->size;
  2109.       int arrow_end;
  2110.  
  2111.       if (len > width)
  2112.     len = width;
  2113.       for (i = 0; i < len; i++)
  2114.     startp[i] = p[i];
  2115.  
  2116.       /* Bug in SunOS 4.1.1 compiler requires this intermediate variable.  */
  2117.       arrow_end = (startp - desired_glyphs->glyphs[vpos]) + len;
  2118.       if (desired_glyphs->used[vpos] < arrow_end)
  2119.     desired_glyphs->used[vpos] = arrow_end;
  2120.  
  2121.       overlay_arrow_seen = 1;
  2122.     }
  2123.  
  2124.   val.bufpos = pos;
  2125.   val_display_text_line = val;
  2126.   return &val_display_text_line;
  2127. }
  2128.  
  2129. /* Redisplay the menu bar in the frame for window W.  */
  2130.  
  2131. static void
  2132. display_menu_bar (w)
  2133.      struct window *w;
  2134. {
  2135.   Lisp_Object tail;
  2136.   register int vpos = 0;
  2137.   register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  2138.   int maxendcol = FRAME_WIDTH (f);
  2139.   int hpos = 0;
  2140.  
  2141.   if (FRAME_MENU_BAR_LINES (f) <= 0)
  2142.     return;
  2143.  
  2144.   get_display_line (f, vpos, 0);
  2145.  
  2146.   /* If the user has switched buffers or windows, we need to
  2147.      recompute to reflect the new bindings.  But we'll
  2148.      recompute when update_mode_lines is set too; that means
  2149.      that people can use force-mode-line-update to request
  2150.      that the menu bar be recomputed.  The adverse effect on
  2151.      the rest of the redisplay algorithm is about the same as
  2152.      windows_or_buffers_changed anyway.  */
  2153.   if (windows_or_buffers_changed
  2154.       || update_mode_lines
  2155.       || (XFASTINT (w->last_modified) < MODIFF
  2156.       && (XFASTINT (w->last_modified)
  2157.           <= XBUFFER (w->buffer)->save_modified)))
  2158.     {
  2159.       struct buffer *prev = current_buffer;
  2160.       current_buffer = XBUFFER (w->buffer);
  2161.       FRAME_MENU_BAR_ITEMS (f) = menu_bar_items ();
  2162.       current_buffer = prev;
  2163.     }
  2164.  
  2165.   for (tail = FRAME_MENU_BAR_ITEMS (f); CONSP (tail); tail = XCONS (tail)->cdr)
  2166.     {
  2167.       Lisp_Object string;
  2168.  
  2169.       string = XCONS (XCONS (XCONS (tail)->car)->cdr)->car;
  2170.  
  2171.       /* Record in each item its hpos.  */
  2172.       XFASTINT (XCONS (XCONS (XCONS (tail)->car)->cdr)->cdr) = hpos;
  2173.  
  2174.       if (hpos < maxendcol)
  2175.     hpos = display_string (XWINDOW (FRAME_ROOT_WINDOW (f)), vpos,
  2176.                    XSTRING (string)->data,
  2177.                    hpos, 0, hpos, maxendcol);
  2178.       /* Put a gap of 3 spaces between items.  */
  2179.       if (hpos < maxendcol)
  2180.     {
  2181.       int hpos1 = hpos + 3;
  2182.       hpos = display_string (w, vpos, "", hpos, 0,
  2183.                  min (hpos1, maxendcol), maxendcol);
  2184.     }
  2185.     }
  2186.  
  2187.   FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
  2188.   FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
  2189.  
  2190.   /* Fill out the line with spaces.  */
  2191.   if (maxendcol > hpos)
  2192.     hpos = display_string (w, vpos, "", hpos, 0, maxendcol, -1);
  2193. }
  2194.  
  2195. /* Display the mode line for window w */
  2196.  
  2197. static void
  2198. display_mode_line (w)
  2199.      struct window *w;
  2200. {
  2201.   register int vpos = XFASTINT (w->height) + XFASTINT (w->top) - 1;
  2202.   register int left = XFASTINT (w->left);
  2203.   register int right = XFASTINT (w->width) + left;
  2204.   register FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  2205.  
  2206.   line_number_displayed = 0;
  2207.  
  2208.   get_display_line (f, vpos, left);
  2209.   display_mode_element (w, vpos, left, 0, right, right,
  2210.             current_buffer->mode_line_format);
  2211.   FRAME_DESIRED_GLYPHS (f)->bufp[vpos] = 0;
  2212.  
  2213.   /* Make the mode line inverse video if the entire line
  2214.      is made of mode lines.
  2215.      I.e. if this window is full width,
  2216.      or if it is the child of a full width window
  2217.      (which implies that that window is split side-by-side
  2218.      and the rest of this line is mode lines of the sibling windows).  */
  2219.   if (XFASTINT (w->width) == FRAME_WIDTH (f)
  2220.       || XFASTINT (XWINDOW (w->parent)->width) == FRAME_WIDTH (f))
  2221.     FRAME_DESIRED_GLYPHS (f)->highlight[vpos] = mode_line_inverse_video;
  2222.  
  2223. #ifdef HAVE_X_WINDOWS
  2224.   /* I'm trying this out because I saw Unimpress use it, but it's
  2225.      possible that this may mess adversely with some window managers.  -jla
  2226.  
  2227.      Wouldn't it be nice to use something like mode-line-format to
  2228.      describe frame titles?  -JimB  */
  2229.  
  2230.   /* Change the title of the frame to the name of the buffer displayed
  2231.      in the currently selected window.  Don't do this for minibuffer frames,
  2232.      and don't do it when there's only one non-minibuffer frame.  */
  2233.   if (FRAME_X_P (f)
  2234.       && ! FRAME_MINIBUF_ONLY_P (f)
  2235.       && w == XWINDOW (f->selected_window))
  2236.     x_implicitly_set_name (f, (EQ (Fnext_frame (WINDOW_FRAME (w), Qnil),
  2237.                    WINDOW_FRAME (w))
  2238.                    ? Qnil
  2239.                    : XBUFFER (w->buffer)->name),
  2240.                Qnil);
  2241. #endif
  2242. }
  2243.  
  2244. /* Contribute ELT to the mode line for window W.
  2245.    How it translates into text depends on its data type.
  2246.  
  2247.    VPOS is the position of the mode line being displayed.
  2248.  
  2249.    HPOS is the position (absolute on frame) where this element's text
  2250.    should start.  The output is truncated automatically at the right
  2251.    edge of window W.
  2252.  
  2253.    DEPTH is the depth in recursion.  It is used to prevent
  2254.    infinite recursion here.
  2255.  
  2256.    MINENDCOL is the hpos before which the element may not end.
  2257.    The element is padded at the right with spaces if nec
  2258.    to reach this column.
  2259.  
  2260.    MAXENDCOL is the hpos past which this element may not extend.
  2261.    If MINENDCOL is > MAXENDCOL, MINENDCOL takes priority.
  2262.    (This is necessary to make nested padding and truncation work.)
  2263.  
  2264.    Returns the hpos of the end of the text generated by ELT.
  2265.    The next element will receive that value as its HPOS arg,
  2266.    so as to concatenate the elements.  */
  2267.  
  2268. static int
  2269. display_mode_element (w, vpos, hpos, depth, minendcol, maxendcol, elt)
  2270.      struct window *w;
  2271.      register int vpos, hpos;
  2272.      int depth;
  2273.      int minendcol;
  2274.      register int maxendcol;
  2275.      register Lisp_Object elt;
  2276. {
  2277.  tail_recurse:
  2278.   if (depth > 10)
  2279.     goto invalid;
  2280.  
  2281.   depth++;
  2282.  
  2283. #ifdef SWITCH_ENUM_BUG
  2284.   switch ((int) XTYPE (elt))
  2285. #else
  2286.   switch (XTYPE (elt))
  2287. #endif
  2288.     {
  2289.     case Lisp_String:
  2290.       {
  2291.     /* A string: output it and check for %-constructs within it.  */
  2292.     register unsigned char c;
  2293.     register unsigned char *this = XSTRING (elt)->data;
  2294.  
  2295.     while (hpos < maxendcol && *this)
  2296.       {
  2297.         unsigned char *last = this;
  2298.         while ((c = *this++) != '\0' && c != '%')
  2299.           ;
  2300.         if (this - 1 != last)
  2301.           {
  2302.         register int lim = --this - last + hpos;
  2303.         hpos = display_string (w, vpos, last, hpos, 0, hpos,
  2304.                        min (lim, maxendcol));
  2305.           }
  2306.         else /* c == '%' */
  2307.           {
  2308.         register int spec_width = 0;
  2309.  
  2310.         /* We can't allow -ve args due to the "%-" construct */
  2311.         /* Argument specifies minwidth but not maxwidth
  2312.            (maxwidth can be specified by
  2313.              (<negative-number> . <stuff>) mode-line elements) */
  2314.  
  2315.         while ((c = *this++) >= '0' && c <= '9')
  2316.           {
  2317.             spec_width = spec_width * 10 + (c - '0');
  2318.           }
  2319.  
  2320.         spec_width += hpos;
  2321.         if (spec_width > maxendcol)
  2322.           spec_width = maxendcol;
  2323.  
  2324.         if (c == 'M')
  2325.           hpos = display_mode_element (w, vpos, hpos, depth,
  2326.                            spec_width, maxendcol,
  2327.                            Vglobal_mode_string);
  2328.         else if (c != 0)
  2329.           hpos = display_string (w, vpos,
  2330.                      decode_mode_spec (w, c,
  2331.                                maxendcol - hpos),
  2332.                      hpos, 0, spec_width, maxendcol);
  2333.           }
  2334.       }
  2335.       }
  2336.       break;
  2337.  
  2338.     case Lisp_Symbol:
  2339.       /* A symbol: process the value of the symbol recursively
  2340.      as if it appeared here directly.  Avoid error if symbol void.
  2341.      Special case: if value of symbol is a string, output the string
  2342.      literally.  */
  2343.       {
  2344.     register Lisp_Object tem;
  2345.     tem = Fboundp (elt);
  2346.     if (!NILP (tem))
  2347.       {
  2348.         tem = Fsymbol_value (elt);
  2349.         /* If value is a string, output that string literally:
  2350.            don't check for % within it.  */
  2351.         if (XTYPE (tem) == Lisp_String)
  2352.           hpos = display_string (w, vpos, XSTRING (tem)->data,
  2353.                      hpos, 0, minendcol, maxendcol);
  2354.         /* Give up right away for nil or t.  */
  2355.         else if (!EQ (tem, elt))
  2356.           { elt = tem; goto tail_recurse; }
  2357.       }
  2358.       }
  2359.       break;
  2360.  
  2361.     case Lisp_Cons:
  2362.       {
  2363.     register Lisp_Object car, tem;
  2364.  
  2365.     /* A cons cell: three distinct cases.
  2366.        If first element is a string or a cons, process all the elements
  2367.        and effectively concatenate them.
  2368.        If first element is a negative number, truncate displaying cdr to
  2369.        at most that many characters.  If positive, pad (with spaces)
  2370.        to at least that many characters.
  2371.        If first element is a symbol, process the cadr or caddr recursively
  2372.        according to whether the symbol's value is non-nil or nil.  */
  2373.     car = XCONS (elt)->car;
  2374.     if (XTYPE (car) == Lisp_Symbol)
  2375.       {
  2376.         tem = Fboundp (car);
  2377.         elt = XCONS (elt)->cdr;
  2378.         if (XTYPE (elt) != Lisp_Cons)
  2379.           goto invalid;
  2380.         /* elt is now the cdr, and we know it is a cons cell.
  2381.            Use its car if CAR has a non-nil value.  */
  2382.         if (!NILP (tem))
  2383.           {
  2384.         tem = Fsymbol_value (car);
  2385.         if (!NILP (tem))
  2386.           { elt = XCONS (elt)->car; goto tail_recurse; }
  2387.           }
  2388.         /* Symbol's value is nil (or symbol is unbound)
  2389.            Get the cddr of the original list
  2390.            and if possible find the caddr and use that.  */
  2391.         elt = XCONS (elt)->cdr;
  2392.         if (NILP (elt))
  2393.           break;
  2394.         else if (XTYPE (elt) != Lisp_Cons)
  2395.           goto invalid;
  2396.         elt = XCONS (elt)->car;
  2397.         goto tail_recurse;
  2398.       }
  2399.     else if (XTYPE (car) == Lisp_Int)
  2400.       {
  2401.         register int lim = XINT (car);
  2402.         elt = XCONS (elt)->cdr;
  2403.         if (lim < 0)
  2404.           /* Negative int means reduce maximum width.
  2405.          DO NOT change MINENDCOL here!
  2406.          (20 -10 . foo) should truncate foo to 10 col
  2407.          and then pad to 20.  */
  2408.           maxendcol = min (maxendcol, hpos - lim);
  2409.         else if (lim > 0)
  2410.           {
  2411.         /* Padding specified.  Don't let it be more than
  2412.            current maximum.  */
  2413.         lim += hpos;
  2414.         if (lim > maxendcol)
  2415.           lim = maxendcol;
  2416.         /* If that's more padding than already wanted, queue it.
  2417.            But don't reduce padding already specified even if
  2418.            that is beyond the current truncation point.  */
  2419.         if (lim > minendcol)
  2420.           minendcol = lim;
  2421.           }
  2422.         goto tail_recurse;
  2423.       }
  2424.     else if (XTYPE (car) == Lisp_String || XTYPE (car) == Lisp_Cons)
  2425.       {
  2426.         register int limit = 50;
  2427.         /* LIMIT is to protect against circular lists.  */
  2428.         while (XTYPE (elt) == Lisp_Cons && --limit > 0
  2429.            && hpos < maxendcol)
  2430.           {
  2431.         hpos = display_mode_element (w, vpos, hpos, depth,
  2432.                          hpos, maxendcol,
  2433.                          XCONS (elt)->car);
  2434.         elt = XCONS (elt)->cdr;
  2435.           }
  2436.       }
  2437.       }
  2438.       break;
  2439.  
  2440.     default:
  2441.     invalid:
  2442.       return (display_string (w, vpos, "*invalid*", hpos, 0,
  2443.                   minendcol, maxendcol));
  2444.     }
  2445.  
  2446. #if 0
  2447.  end:
  2448. #endif
  2449.   if (minendcol > hpos)
  2450.     hpos = display_string (w, vpos, "", hpos, 0, minendcol, -1);
  2451.   return hpos;
  2452. }
  2453.  
  2454. /* Return a string for the output of a mode line %-spec for window W,
  2455.    generated by character C and width MAXWIDTH.  */
  2456.  
  2457. static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
  2458.  
  2459. static char *
  2460. decode_mode_spec (w, c, maxwidth)
  2461.      struct window *w;
  2462.      register char c;
  2463.      register int maxwidth;
  2464. {
  2465.   Lisp_Object obj = Qnil;
  2466.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  2467.   char *decode_mode_spec_buf = (char *) FRAME_TEMP_GLYPHS (f)->total_contents;
  2468.  
  2469.   if (maxwidth > FRAME_WIDTH (f))
  2470.     maxwidth = FRAME_WIDTH (f);
  2471.  
  2472.   switch (c)
  2473.     {
  2474.     case 'b': 
  2475.       obj = current_buffer->name;
  2476. #if 0
  2477.       if (maxwidth >= 3 && XSTRING (obj)->size > maxwidth)
  2478.     {
  2479.       bcopy (XSTRING (obj)->data, decode_mode_spec_buf, maxwidth - 1);
  2480.       decode_mode_spec_buf[maxwidth - 1] = '\\';
  2481.       decode_mode_spec_buf[maxwidth] = '\0';
  2482.       return decode_mode_spec_buf;
  2483.     }
  2484. #endif
  2485.       break;
  2486.  
  2487.     case 'f': 
  2488.       obj = current_buffer->filename;
  2489. #if 0
  2490.       if (NILP (obj))
  2491.     return "[none]";
  2492.       else if (XTYPE (obj) == Lisp_String && XSTRING (obj)->size > maxwidth)
  2493.     {
  2494.       bcopy ("...", decode_mode_spec_buf, 3);
  2495.       bcopy (XSTRING (obj)->data + XSTRING (obj)->size - maxwidth + 3,
  2496.          decode_mode_spec_buf + 3, maxwidth - 3);
  2497.       return decode_mode_spec_buf;
  2498.     }
  2499. #endif
  2500.       break;
  2501.  
  2502.     case 'l':
  2503.       {
  2504.     int startpos = marker_position (w->start);
  2505.     int line, linepos, topline;
  2506.     int nlines, junk;
  2507.     int height = XFASTINT (w->height);
  2508.  
  2509.     /* If we decided that this buffer isn't suitable for line numbers, 
  2510.        don't forget that too fast.  */
  2511.     if (EQ (w->base_line_pos, w->buffer))
  2512.       return "??";
  2513.  
  2514.     /* If the buffer is very big, don't waste time.  */
  2515.     if (ZV - BEGV > line_number_display_limit)
  2516.       {
  2517.         w->base_line_pos = Qnil;
  2518.         w->base_line_number = Qnil;
  2519.         return "??";
  2520.       }
  2521.  
  2522.     if (!NILP (w->base_line_number)
  2523.         && !NILP (w->base_line_pos)
  2524.         && XFASTINT (w->base_line_pos) <= marker_position (w->start))
  2525.       {
  2526.         line = XFASTINT (w->base_line_number);
  2527.         linepos = XFASTINT (w->base_line_pos);
  2528.       }
  2529.     else
  2530.       {
  2531.         line = 1;
  2532.         linepos = BEGV;
  2533.       }
  2534.  
  2535.     /* Count lines from base line to window start position.  */
  2536.     nlines = display_count_lines (linepos, startpos, startpos, &junk);
  2537.  
  2538.     topline = nlines + line;
  2539.  
  2540.     /* Determine a new base line, if the old one is too close
  2541.        or too far away, or if we did not have one.
  2542.        "Too close" means it's plausible a scroll-down would
  2543.        go back past it.  */
  2544.     if (startpos == BEGV)
  2545.       {
  2546.         XFASTINT (w->base_line_number) = topline;
  2547.         XFASTINT (w->base_line_pos) = BEGV;
  2548.       }
  2549.     else if (nlines < height + 25 || nlines > height * 3 + 50
  2550.          || linepos == BEGV)
  2551.       {
  2552.         int limit = BEGV;
  2553.         int position;
  2554.         int distance = (height * 2 + 30) * 200;
  2555.  
  2556.         if (startpos - distance > limit)
  2557.           limit = startpos - distance;
  2558.  
  2559.         nlines = display_count_lines (startpos, limit,
  2560.                       -(height * 2 + 30),
  2561.                       &position);
  2562.         /* If we couldn't find the lines we wanted within 
  2563.            200 chars per line,
  2564.            give up on line numbers for this window.  */
  2565.         if (position == startpos - distance)
  2566.           {
  2567.         w->base_line_pos = w->buffer;
  2568.         w->base_line_number = Qnil;
  2569.         return "??";
  2570.           }
  2571.  
  2572.         XFASTINT (w->base_line_number) = topline - nlines;
  2573.         XFASTINT (w->base_line_pos) = position;
  2574.       }
  2575.  
  2576.     /* Now count lines from the start pos to point.  */
  2577.     nlines = display_count_lines (startpos, PT, PT, &junk);
  2578.  
  2579.     /* Record that we did display the line number.  */
  2580.     line_number_displayed = 1;
  2581.  
  2582.     /* Make the string to show.  */
  2583.     sprintf (decode_mode_spec_buf, "%d", topline + nlines);
  2584.     return decode_mode_spec_buf;
  2585.       }
  2586.       break;
  2587.  
  2588.     case 'm': 
  2589.       obj = current_buffer->mode_name;
  2590.       break;
  2591.  
  2592.     case 'n':
  2593.       if (BEGV > BEG || ZV < Z)
  2594.     return " Narrow";
  2595.       break;
  2596.  
  2597.     case '*':
  2598.       if (!NILP (current_buffer->read_only))
  2599.     return "%";
  2600.       if (MODIFF > current_buffer->save_modified)
  2601.     return "*";
  2602.       return "-";
  2603.  
  2604.     case 's':
  2605.       /* status of process */
  2606.       obj = Fget_buffer_process (Fcurrent_buffer ());
  2607.       if (NILP (obj))
  2608.     return "no process";
  2609.       obj = Fsymbol_name (Fprocess_status (obj));
  2610.       break;
  2611.  
  2612.     case 'p':
  2613.       {
  2614.     int pos = marker_position (w->start);
  2615.     int total = ZV - BEGV;
  2616.  
  2617.     if (XFASTINT (w->window_end_pos) <= Z - ZV)
  2618.       {
  2619.         if (pos <= BEGV)
  2620.           return "All";
  2621.         else
  2622.           return "Bottom";
  2623.       }
  2624.     else if (pos <= BEGV)
  2625.       return "Top";
  2626.     else
  2627.       {
  2628.         total = ((pos - BEGV) * 100 + total - 1) / total;
  2629.         /* We can't normally display a 3-digit number,
  2630.            so get us a 2-digit number that is close.  */
  2631.         if (total == 100)
  2632.           total = 99;
  2633.         sprintf (decode_mode_spec_buf, "%2d%%", total);
  2634.         return decode_mode_spec_buf;
  2635.       }
  2636.       }
  2637.  
  2638.     case '%':
  2639.       return "%";
  2640.  
  2641.     case '[': 
  2642.       {
  2643.     int i;
  2644.     char *p;
  2645.  
  2646.     if (command_loop_level > 5)
  2647.       return "[[[... ";
  2648.     p = decode_mode_spec_buf;
  2649.     for (i = 0; i < command_loop_level; i++)
  2650.       *p++ = '[';
  2651.     *p = 0;
  2652.     return decode_mode_spec_buf;
  2653.       }
  2654.  
  2655.     case ']': 
  2656.       {
  2657.     int i;
  2658.     char *p;
  2659.  
  2660.     if (command_loop_level > 5)
  2661.       return " ...]]]";
  2662.     p = decode_mode_spec_buf;
  2663.     for (i = 0; i < command_loop_level; i++)
  2664.       *p++ = ']';
  2665.     *p = 0;
  2666.     return decode_mode_spec_buf;
  2667.       }
  2668.       
  2669.     case '-':
  2670.       {
  2671.     register char *p;
  2672.     register int i;
  2673.     
  2674.     if (maxwidth < sizeof (lots_of_dashes))
  2675.       return lots_of_dashes;
  2676.     else
  2677.       {
  2678.         for (p = decode_mode_spec_buf, i = maxwidth; i > 0; i--)
  2679.           *p++ = '-';
  2680.         *p = '\0';
  2681.       }
  2682.     return decode_mode_spec_buf;
  2683.       }
  2684.     }
  2685.   
  2686.   if (XTYPE (obj) == Lisp_String)
  2687.     return (char *) XSTRING (obj)->data;
  2688.   else
  2689.     return "";
  2690. }
  2691.  
  2692. /* Count up to N lines starting from FROM.
  2693.    But don't go beyond LIMIT.
  2694.    Return the number of lines thus found (always positive).
  2695.    Store the position after what was found into *POS_PTR.  */
  2696.  
  2697. static int
  2698. display_count_lines (from, limit, n, pos_ptr)
  2699.      int from, limit, n;
  2700.      int *pos_ptr;
  2701. {
  2702.   int oldbegv = BEGV;
  2703.   int oldzv = ZV;
  2704.   int shortage = 0;
  2705.   
  2706.   if (limit < from)
  2707.     BEGV = limit;
  2708.   else
  2709.     ZV = limit;
  2710.  
  2711.   *pos_ptr = scan_buffer ('\n', from, n, &shortage);
  2712.  
  2713.   ZV = oldzv;
  2714.   BEGV = oldbegv;
  2715.  
  2716.   if (n < 0)
  2717.     /* When scanning backwards, scan_buffer stops *after* the last newline
  2718.        it finds, but does count it.  Compensate for that.  */
  2719.     return - n - shortage - (*pos_ptr != limit);
  2720.   return n - shortage;
  2721. }  
  2722.  
  2723. /* Display STRING on one line of window W, starting at HPOS.
  2724.    Display at position VPOS.  Caller should have done get_display_line.
  2725.    If VPOS == -1, display it as the current frame's title.
  2726.  
  2727.   TRUNCATE is GLYPH to display at end if truncated.  Zero for none.
  2728.  
  2729.   MINCOL is the first column ok to end at.  (Pad with spaces to this col.)
  2730.   MAXCOL is the last column ok to end at.  Truncate here.
  2731.     -1 for MINCOL or MAXCOL means no explicit minimum or maximum.
  2732.   Both count from the left edge of the frame, as does HPOS.
  2733.   The right edge of W is an implicit maximum.
  2734.   If TRUNCATE is nonzero, the implicit maximum is one column before the edge.
  2735.  
  2736.   Returns ending hpos */
  2737.  
  2738. static int
  2739. display_string (w, vpos, string, hpos, truncate, mincol, maxcol)
  2740.      struct window *w;
  2741.      unsigned char *string;
  2742.      int vpos, hpos;
  2743.      GLYPH truncate;
  2744.      int mincol, maxcol;
  2745. {
  2746.   register int c;
  2747.   register GLYPH *p1;
  2748.   int hscroll = XINT (w->hscroll);
  2749.   int tab_width = XINT (XBUFFER (w->buffer)->tab_width);
  2750.   register GLYPH *start;
  2751.   register GLYPH *end;
  2752.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  2753.   struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (f);
  2754.   GLYPH *p1start = desired_glyphs->glyphs[vpos] + hpos;
  2755.   int window_width = XFASTINT (w->width);
  2756.  
  2757.   /* Use the standard display table, not the window's display table.
  2758.      We don't want the mode line in rot13.  */
  2759.   register struct Lisp_Vector *dp = 0;
  2760.  
  2761.   if (XTYPE (Vstandard_display_table) == Lisp_Vector
  2762.       && XVECTOR (Vstandard_display_table)->size == DISP_TABLE_SIZE)
  2763.     dp = XVECTOR (Vstandard_display_table);
  2764.  
  2765.   if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
  2766.  
  2767.   p1 = p1start;
  2768.   start = desired_glyphs->glyphs[vpos] + XFASTINT (w->left);
  2769.   end = start + window_width - (truncate != 0);
  2770.  
  2771.   if ((window_width + XFASTINT (w->left)) != FRAME_WIDTH (f))
  2772.     {
  2773.       if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
  2774.     {
  2775.       int i;
  2776.  
  2777.       for (i = 0; i < VERTICAL_SCROLL_BAR_WIDTH; i++)
  2778.         *end-- = ' ';
  2779.     }
  2780.       else
  2781.     *end-- = '|';
  2782.     }
  2783.  
  2784.   if (maxcol >= 0 && end - desired_glyphs->glyphs[vpos] > maxcol)
  2785.     end = desired_glyphs->glyphs[vpos] + maxcol;
  2786.   if (maxcol >= 0 && mincol > maxcol)
  2787.     mincol = maxcol;
  2788.  
  2789.   while (p1 < end)
  2790.     {
  2791.       c = *string++;
  2792.       if (!c) break;
  2793.       if (c >= 040 && c < 0177
  2794.       && (dp == 0 || XTYPE (DISP_CHAR_VECTOR (dp, c)) != Lisp_Vector))
  2795.     {
  2796.       if (p1 >= start)
  2797.         *p1 = c;
  2798.       p1++;
  2799.     }
  2800.       else if (c == '\t')
  2801.     {
  2802.       do
  2803.         {
  2804.           if (p1 >= start && p1 < end)
  2805.         *p1 = SPACEGLYPH;
  2806.           p1++;
  2807.         }
  2808.       while ((p1 - start + hscroll - (hscroll > 0)) % tab_width);
  2809.     }
  2810.       else if (dp != 0 && XTYPE (DISP_CHAR_VECTOR (dp, c)) == Lisp_Vector)
  2811.         p1 = copy_rope (p1, start, DISP_CHAR_VECTOR (dp, c), 0);
  2812.       else if (c < 0200 && ! NILP (buffer_defaults.ctl_arrow))
  2813.     {
  2814.       if (p1 >= start)
  2815.         *p1 = (dp && XTYPE (DISP_CTRL_GLYPH (dp)) == Lisp_Int
  2816.            ? XINT (DISP_CTRL_GLYPH (dp)) : '^');
  2817.       p1++;
  2818.       if (p1 >= start && p1 < end)
  2819.         *p1 = c ^ 0100;
  2820.       p1++;
  2821.     }
  2822.       else
  2823.     {
  2824.       if (p1 >= start)
  2825.         *p1 = (dp && XTYPE (DISP_ESCAPE_GLYPH (dp)) == Lisp_Int
  2826.            ? XINT (DISP_ESCAPE_GLYPH (dp)) : '\\');
  2827.       p1++;
  2828.       if (p1 >= start && p1 < end)
  2829.         *p1 = (c >> 6) + '0';
  2830.       p1++;
  2831.       if (p1 >= start && p1 < end)
  2832.         *p1 = (7 & (c >> 3)) + '0';
  2833.       p1++;
  2834.       if (p1 >= start && p1 < end)
  2835.         *p1 = (7 & c) + '0';
  2836.       p1++;
  2837.     }
  2838.     }
  2839.  
  2840.   if (c)
  2841.     {
  2842.       p1 = end;
  2843.       if (truncate) *p1++ = truncate;
  2844.     }
  2845.   else if (mincol >= 0)
  2846.     {
  2847.       end = desired_glyphs->glyphs[vpos] + mincol;
  2848.       while (p1 < end)
  2849.     *p1++ = SPACEGLYPH;
  2850.     }
  2851.  
  2852.   {
  2853.     register int len = p1 - desired_glyphs->glyphs[vpos];
  2854.  
  2855.     if (len > desired_glyphs->used[vpos])
  2856.       desired_glyphs->used[vpos] = len;
  2857.     desired_glyphs->glyphs[vpos][desired_glyphs->used[vpos]] = 0;
  2858.  
  2859.     return len;
  2860.   }
  2861. }
  2862.  
  2863. void
  2864. syms_of_xdisp ()
  2865. {
  2866.   staticpro (&last_arrow_position);
  2867.   staticpro (&last_arrow_string);
  2868.   last_arrow_position = Qnil;
  2869.   last_arrow_string = Qnil;
  2870.  
  2871.   DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
  2872.     "String (or mode line construct) included (normally) in `mode-line-format'.");
  2873.   Vglobal_mode_string = Qnil;
  2874.  
  2875.   DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
  2876.     "Marker for where to display an arrow on top of the buffer text.\n\
  2877. This must be the beginning of a line in order to work.\n\
  2878. See also `overlay-arrow-string'.");
  2879.   Voverlay_arrow_position = Qnil;
  2880.  
  2881.   DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
  2882.     "String to display as an arrow.  See also `overlay-arrow-position'.");
  2883.   Voverlay_arrow_string = Qnil;
  2884.  
  2885.   DEFVAR_INT ("scroll-step", &scroll_step,
  2886.     "*The number of lines to try scrolling a window by when point moves out.\n\
  2887. If that fails to bring point back on frame, point is centered instead.\n\
  2888. If this is zero, point is always centered after it moves off frame.");
  2889.  
  2890.   DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
  2891.  
  2892.   DEFVAR_BOOL ("truncate-partial-width-windows",
  2893.            &truncate_partial_width_windows,
  2894.     "*Non-nil means truncate lines in all windows less than full frame wide.");
  2895.   truncate_partial_width_windows = 1;
  2896.  
  2897.   DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
  2898.     "*Non-nil means use inverse video for the mode line.");
  2899.   mode_line_inverse_video = 1;
  2900.  
  2901.   DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
  2902.     "*Maximum buffer size for which line number should be displayed.");
  2903.   line_number_display_limit = 1000000;
  2904.  
  2905.   DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
  2906.     "*Non-nil means highlight region even in nonselected windows.");
  2907.   highlight_nonselected_windows = 1;
  2908. }
  2909.  
  2910. /* initialize the window system */
  2911. _VOID_
  2912. init_xdisp ()
  2913. {
  2914.   Lisp_Object root_window;
  2915. #ifndef COMPILER_REGISTER_BUG
  2916.   register
  2917. #endif /* COMPILER_REGISTER_BUG */
  2918.     struct window *mini_w;
  2919.  
  2920.   this_line_bufpos = 0;
  2921.  
  2922.   mini_w = XWINDOW (minibuf_window);
  2923.   root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
  2924.  
  2925.   echo_area_glyphs = 0;
  2926.   previous_echo_glyphs = 0;
  2927.  
  2928.   if (!noninteractive)
  2929.     {
  2930.       FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
  2931.       XFASTINT (XWINDOW (root_window)->top) = 0;
  2932.       set_window_height (root_window, FRAME_HEIGHT (f) - 1, 0);
  2933.       XFASTINT (mini_w->top) = FRAME_HEIGHT (f) - 1;
  2934.       set_window_height (minibuf_window, 1, 0);
  2935.  
  2936.       XFASTINT (XWINDOW (root_window)->width) = FRAME_WIDTH (f);
  2937.       XFASTINT (mini_w->width) = FRAME_WIDTH (f);
  2938.     }
  2939. }
  2940.